一步步学习ASP.NET MVC3 (13)——HTML辅助方法

请注明转载地址:http://www.cnblogs.com/arhat

今天老魏是在十分郁闷,个人一个U盘丢了,心疼里面的资料啊,所有是老魏辛辛苦苦积攒的Linux资料,太心疼,到如今心情还不是很爽。没办法,丢了也就丢了。但愿老魏可以从服务器中找到这些备份的资料吧。css

那么开始今天的章节,因为前两天比较忙,老魏更新的慢了,之后慢慢不上来吧!今天咱们要说的是ASP.NET MVC 中的HTML辅助方法。HTML辅助方法可以帮助咱们可以快速生成视图代码,经过HTML辅助方法能够向像编写C#同样编写HTML文件。html

这些辅助方法都位于System.Web.Mvc.Html这个命名空间,你们能够从这个命名空间中查看这些方法。当了,因为这些辅助方法只是用来生成html内容的,因此老魏这里呢就再也不详细的介绍,根据下面我举的例子,你们能够依葫芦画瓢看着帮助文档来学习。服务器

而HTML辅助方法是HTMLHelper类的扩展方法,因此本章咱们主要来看看这些辅助方法是如何帮助咱们快速的开发视图文件。app

1、超连接
HTMLHelper的扩展方法提供如下的扩展方法能够生成超连接。 函数

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName);        

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues);        

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues);        

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName);        

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes);

这么多重载方法咱们并不须要所有的记住,而是在适当的时候使用适当的方法。咱们来学习一下经常使用的方法。post

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName);

这个方法是用来跳转到对应的action。学习

@Html.ActionLink("这是超链接", "Index");
<a href="/">这是超链接</a>

可是须要注意的是的,actionName只能是视图所在的控制下的Action方法。this

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues);

这个方法生成的超链接能够传递一下参数。spa

@Html.ActionLink("这是超链接", "Index", new { id=1,name="department"});
<a href="/Home/Index/1?name=department">这是超链接</a>
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName);

这个方法生成的链接能够跨controllercode

@Html.ActionLink("这是超链接", "Test","About");
<a href="/About/Test">这是超链接</a>;
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName,object routeValues, object htmlAttributes);
@Html.ActionLink("这是超链接", "Test", "About", new { name="apple"},null);
<a href="/About/Test?name=apple">这是超链接</a>;

咱们能够经过最后一个参数来给html元素添加属性。

@Html.ActionLink("这是超链接", "Test", "About", new { name = "apple" }, new { style="text-decoration:none"});
<a href="/About/Test?name=apple" style="text-decoration:none">这是超链接</a>;

这里须要注意的地方是若是要给超连接添加一个class的css属性,那么必须在前面加上@符号,由于class是C#的关键字。

@Html.ActionLink("这是超链接", "Test", "About", new { name = "apple" }, new { style="text-decoration:none",@class="a"});
<a class=”a” href="/About/Test?name=apple" style="text-decoration:none">这是超链接</a>;

表单
固然除了超连接,HTML辅助方法也提供了对表单的支持。若是咱们使用辅助方法来生成表单能够经过两种方法,只是风格不一样。
经过Html.BeginForm()方法来声明一个表单。又有BeginForm方法的重载比较多,这里就不一一列举了,咱们来看一下经常使用的重载方法。你们能够从System.Web.Mvc.Html.FormExtensions中查看一下重载的方法。

@using (Html.BeginForm()) 

{ 

<p>

        帐号:@Html.TextBox("username")

</p>

<p>

        密码:@Html.Password("pwd")

</p>

}
<form action="/" method="post">   

 <p>        帐号:<input id="username" name="username" type="text" value="" />    </p>   

 <p>        密码:<input id="pwd" name="pwd" type="password" />    </p>

</form>

那么咱们能够看到生成的表单中action的地址为”/”。因此咱们能够经过其余的重载函数来生成表单。

@using (Html.BeginForm("Test","Home")) 

{ 

<p>

        帐号:@Html.TextBox("username")

</p>

<p>

        密码:@Html.Password("pwd")

</p>

}
<form action="/Home/Test" method="post">   

 <p>        帐号:<input id="username" name="username" type="text" value="" />    </p>    

<p>        密码:<input id="pwd" name="pwd" type="password" />    </p>

</form>

在上面的例子中,咱们使用到了表单元素,那么这些表单元素你们能够从System.Web.Mvc.HtmlInputExtensions中查看,这里老魏就再也不作介绍了(由于比较简单)。

相关文章
相关标签/搜索