代码改变世界

asp.net MVC初学体会.

2009-02-01 23:09  Otis's Technology Space  阅读(1634)  评论(1编辑  收藏  举报
asp.net MVC一直想学习的了. 老赵同学的webcast课程也看了几节,但是一直不大想动手做..今天复习并实践了一下下.. 因为实在没有耐心再去看老赵同学的webcast了.所以就直接看http://www.asp.net/learn/mvc/的文章,记下一些关键.就开始做了..

以下是记下的关键东西.

Understanding Action Results

A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.

The ASP.NET MVC framework supports six standard types of action results:

  1. ViewResult – Represents HTML and markup.
  2. EmptyResult – Represents no result.
  3. RedirectResult – Represents a redirection to a new URL.
  4. RedirectToRouteResult – Represents a redirection to a new controller action.
  5. JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  6. ContentResult – Represents a text result.

 

Understanding Action Filters

Using an Action Filter

  [OutputCache(Duration=10)]     

he Different Types of Filters

The ASP.NET MVC framework supports four different types of filters:

  1. Authorization filters – Implements the IAuthorizationFilter attribute.
  2. Action filters – Implements the IActionFilter attribute.
  3. Result filters – Implements the IResultFilter attribute.
  4. Exception filters – Implements the IExceptionFilter attribute.

http://www.asp.net/learn/mvc/tutorial-14-cs.aspx

mproving Performance with Output Caching (C#)

http://www.asp.net/learn/mvc/tutorial-15-cs.aspx

Authenticating Users with Forms Authentication (C#)

http://www.asp.net/learn/mvc/tutorial-17-cs.aspx

Adding Dynamic Content to a Cached Page (C#)

http://www.asp.net/learn/mvc/tutorial-19-cs.aspx

然后开始做..

我要实现的是一个树形的控件..大概是这样:

先从数据库读出所有数据到DataTable中,然后解析DataTable到实体类中,最后写一个HtmlHelper,绑定实体类的数据.

DB那边先不弄,现在先弄控件.

先定义存放Tree数据的实体类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OtisMVCBlog.Models.ControlsModels
{
public class OtisTreeNode
{
public List<OtisTreeNode> Nodes { get; set; }

public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Target { get; set; }
public string URL { get; set; }

public bool HaveChild()
{
if (this.Nodes != null) return true;
return false;
}
}
}
然后是编写Tree控件.是用HtmlHelper的来弄, 两个helper,一个是生成<ul><li>格式的HTML,另一个是生成Select.代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OtisMVCBlog.Models.ControlsModels;
using System.Text;
namespace OtisMVCBlog.Utils.Helpers
{
public static class TreeHtmlHelpers
{

public static string TreeHtml(this HtmlHelper helper, List<OtisTreeNode> treeNodesList,string elementID, string cssClassPrefix)
{
StringBuilder sbHTML = new StringBuilder(string.Format("<ul id=\"{0}\" class=\"{1}\">",elementID, cssClassPrefix));
int level = 1;
foreach (OtisTreeNode tn in treeNodesList)
{
CreateTree(tn, cssClassPrefix, ref sbHTML, ref level);
}
sbHTML.Append("</ul>");
return sbHTML.ToString();
}
public static string TreeSelect(this HtmlHelper helper, List<OtisTreeNode> treeNodesList,string elementID, string cssClass)
{
StringBuilder sbHTML = new StringBuilder(string.Format("<select id=\"{0}\" class=\"{1}\">\n",elementID, cssClass));
int level = 1;
foreach (OtisTreeNode tn in treeNodesList)
{
CreateSelectTree(tn, cssClass, ref sbHTML, ref level);
}
sbHTML.Append("</select>\n");
return sbHTML.ToString();
}
private static void CreateSelectTree(OtisTreeNode tn, string cssClassPrefix, ref StringBuilder treeHTML, ref int treeLevel)
{

string splitStr = "";
for (int i = 1; i < treeLevel; i++)
{
splitStr += "&nbsp;&nbsp;";
}
treeHTML.Append(string.Format("<option value=\"{0}\">{1}{2}</option>\n",tn.ID,splitStr,tn.Title));
if (tn.HaveChild())
{
StringBuilder sbhtml = treeHTML; int level = treeLevel + 1;
foreach (OtisTreeNode tnItem in tn.Nodes)
{
CreateSelectTree(tnItem, cssClassPrefix, ref sbhtml, ref level);
}

}

}
private static void CreateTree(OtisTreeNode tn, string cssClassPrefix, ref StringBuilder treeHTML, ref int treeLevel)
{
if (string.IsNullOrEmpty(tn.URL) && tn.HaveChild())
{
tn.URL = string.Format("javascript:{0}ShowTree('ul_{1}');",cssClassPrefix,tn.ID);
}
treeHTML.Append(string.Format("<li id=\"li_{0}\" class=\"{1}_li_{2}\"> <a target=\"{3}\" href=\"{4}\">{5}</a>",
tn.ID, cssClassPrefix, treeLevel, tn.Target, tn.URL,tn.Title));
if (tn.HaveChild())
{
treeHTML.Append(string.Format("<ul id=\"ul_{0}\" class=\"{1}_ul_{2}\">",
tn.ID, cssClassPrefix, treeLevel));
StringBuilder sbhtml = treeHTML; int level = treeLevel + 1;
foreach (OtisTreeNode tnItem in tn.Nodes)
{
CreateTree(tnItem, cssClassPrefix,ref sbhtml,ref level);
}
treeHTML.Append("</ul>");
}
treeHTML.Append("</li>");
}
}
}
 以下Code是View测试的.
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="OtisMVCBlog.Models.ControlsModels" %>
<%@ Import Namespace="OtisMVCBlog.Utils.Helpers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<title>Index</title>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/C#" runat="server">

List<OtisTreeNode> tns = new List<OtisTreeNode>();
protected void Page_Load(object s, EventArgs e)
{


for (int i = 0; i < 7; i++)
{
OtisTreeNode otn = GetNode(new Random().Next(1, int.MaxValue), string.Format("Items {0}", new Random().Next(1, int.MaxValue)), true);
if (i % 3 == 0)
{
otn.Nodes = new List<OtisTreeNode>();
for (int j = 0; j < 2; j++)
{
otn.Nodes.Add(GetNode(new Random().Next(1, int.MaxValue), string.Format("Items {0}", new Random().Next(1, int.MaxValue)), true));
}
}
tns.Add(otn);
}

}
private OtisTreeNode GetNode(int id, string title, bool isURL)
{
OtisTreeNode otn = new OtisTreeNode();

otn.ID = id;
otn.Target = "_blank";
otn.Title = title;
if(isURL) otn.URL = "http://www.cnblogs.com";
return otn;
}

</script>
<h2>Index</h2>
<table>
<tr><td>
<% =Html.TreeHtml(tns,"tree","treeCss") %>
</td><td valign="top">
<% =Html.TreeSelect(tns,"treeSelect","treeSelectCss") %>
</td></tr>
</table>
</asp:Content>

最后结果如下:
image 
感觉还是蛮方便的..^_^.
但是当我开始要结合数据库弄增删改功能时.习惯了WebForm的思维与WebForm的方便.现在用MVC弄,感觉要麻烦很多了.
如果是用WebForm,这些功能当然是做在一个页面中.而且可以很快得完成..而MVC像是推荐做成到多个页面中.一个是新增,一个是修改.
而且也没有WebForm的事件驱动那么方便和透明,一切数据都要明确的POST到特定的Controller中..这样还真是有点不习惯..
我得找出一个方便的方法.因为这页如果做成多个页面.对用户体验也不是很好..
研究中..