1/1页1 跳转到查看:629
发新话题 回复该主题
键盘左右键可以进行前后翻页操作
帮助

Asp.net Mvc Framework 八 (Helper)

Asp.net Mvc Framework 八 (Helper)

Asp.netMVC中的Helper  何谓Helper,其实就是在View中为了实现一些灵活功能而写的方法组
  其实Asp.net MVC的View是Aspx的页面,本身可以声明定义方法,那为什么要有Helper呢
  其实无非是将界面与逻辑分离,而且Asp.net MVC也并不只支持Aspx一种View,在扩展包中,也有Castle的NVelocity引擎和Boo所以,如果在Aspx中定义方法的话会影响其扩展性和可移植性.而且代码也不太好看.
  UrlHelper的Action方法 用于生成一个超级链接,它的重载为
  public string Action(string actionName);
  public string Action(string actionName, object values);
  public string Action(string actionName, RouteValueDictionary valuesDictionary);
  public string Action(string actionName, string controllerName);
  public string Action(string actionName, string controllerName, object values);
  public string Action(string actionName, string controllerName, RouteValueDictionary valuesDictionary);
  例如我在View中写Url.Action("Index","Home"),运行后则会生成/Home/Index这个地址
  如果你的系统中的URL Routing规则总是变化的话这个Helper则是你必备之选.
  public string Encode(string url);这也是UrlHelper的一个方法 使用方法 如<%=Url.Encode("中文")%>功能与Server.UrlEncode相同,这里不多说了
  如果你有特殊需要可以用3.0新特性,扩展方法来为UrlHelper来增加新的功能
  HtmlHelper则是另一个常用之Helper
  它是来生成HTML代码用的
  eg.
  <%=Html.ActionLink("首页","index","Home")%>则生成<a href="/Home/Index">首页</a>
  重载方法有:
public string ActionLink(string linkText, string actionName);
public string ActionLink(string linkText, string actionName, object values);
public string ActionLink(string linkText, string actionName, RouteValueDictionary valuesDictionary);
public string ActionLink(string linkText, string actionName, string controllerName);
public string ActionLink(string linkText, string actionName, string controllerName, object values);    public string ActionLink(string linkText, string actionName, string controllerName, RouteValueDictionary valuesDictionary);当然HTMLHelper的种类就比UrlHelper多得多了
  比如有Button
  <%=Html.Button("name","value","onclick") %>
  生成
  <button  name="name" id="name">value</button>
  CheckBox:
  <%=Html.CheckBox("name","text") %>生成
  <input value="text" name="name" type="checkbox"> text
  Form:
<%=Html.Form("Home","Index",FormMethod.Post) %>生成
<form action="/Home" method="post">System.Web.Mvc.SimpleForm
</form>
  当然还有类似于SubmitButton,Image这些方法,这里就不多讲了
  注意一点Preview2中Html.Mailto方法有些Bug请尽量避免使用这个方法

TOP

 
1/1页1 跳转到
发表新主题 回复该主题