MVC HtmlHelper类的方法总结(转)

编辑器加载中...

HtmlHelper类的辅助和扩展方法:

(1)ActionLink 生成一个特定的控制器行为连接 <%=Html.ActionLink("Edit", "Edit", "Book", new { id = Model.ID }, new { @class = "BookDetail"})%> 具体说明: Edit,为linkText,具体而言就是显示的字符串 Edit,对应为ActionName;   Book,为Controller;   new { id = Model.ID },为生成元素的id定义;   new { @class = “BookDetail” },则为元素添加了tag要素

(2)AntiForgeryToken 生成一个隐藏的表单字段,可与ValidateAntiForgeryToken属性一起使用以确保Request没有被修改 具体说明: 如果你要保护你的应用程序的多个形式的相互独立,AntiForgeryToken 将生成不同的防伪标志 <%= Html.AntiForgeryToken("someArbitraryString") %> [ValidateAntiForgeryToken(Salt="someArbitraryString")] public ViewResult SubmitUpdate() { // ... etc }

(3)AttributeEncode 数据属性编码 :提供了HtmlAttributeEncode功能 具体说明: <%= Html.AttributeEncode(Url.Action("Login")) %>

(4)DropDownList 基于一对键值对生成DropDownList 具体说明: Html.DropDownList( string name, IEnumerable selectList, string optionLabel, object htmlAttributes) name:选择元素的名称,绑定到元素视图的模型属性 selectList:选项列表, optionLabel:类似与“Select …” 可选的默认的标签 htmlAttributes:可添加的属性 使用方法 代码 List items = new List(); items.Add( new SelectListItem { Text = "Swimming", Value = "1" }); items.Add(new SelectListItem { Text = "Cycling", Value = "2", Selected = true }); items.Add(new SelectListItem { Text = "Running", Value = "3" }); ViewData["ListItems"] = items; <%= Html.DropDownList("ListItems") %>

(5)Encode 编码字符串,以防跨站脚本攻击 使用方法 < %=Html.Encode("< script src=\"j . JS\">< div background='javascript:alert('');'/>")%> 编码结果 <script src="j . JS"></script><div background='javascript:alert('');'/>

(6)Hidden 生成表单隐藏字段 <%= Html.Hidden("SearchType", 1) %> 前台源代码:

(7)ListBox 基于列键值对生成下拉框 使用方法: 代码 <%= Html.ListBox( "optionsList", new [] { new SelectListItem { Text = "Option 1", Value = "opt1" }, new SelectListItem { Text = "Option 2", Value = "opt2" }, new SelectListItem { Text = "Option 3", Value = "opt3" } }, "Choose an option" ) %> 前台源代码:

(8)Pasword 生成表单密码字段 使用方法: <%= Html.Password("password", ViewData["password"]) %> (9)TextBox 生成文本框 使用方法: <%=Html.TextBox("Name", Model.Contact.Name)%> 前台源代码:

(10)Button 生成一个按钮 最新发布2.0.0.0MVC的已经没有了

(11)CheckBox 生成checkBox 使用方法: <%= Html.CheckBox("a") %> 前台源代码:

(12)BeginForm 生成的HTML表单 使用方法: <% using (Html.BeginForm(Model.PostAction, "Home", FormMethod.Post)) { %> 前台源代码:

(13)Image 生成图像标签 引用一张图片 <%=Html.Image("~/Images/bigwave.jpg") %>
设置名称与宽度 <%=Html.Image("~/Images/bigwave.jpg", "myWave", new { width = "30px" })%>
(14)Mailto 生成Email连接 Simple Email: <%=Html.MailTo("mailto:weflytotti@163.com%22,%22Email Me!") %> With Subject and Body <%=Html.MailTo("mailto:weflytotti@163.com%22,%22Email Me!","Sending you a note" ,"Hey - wanted to say hi!") %>
(15)RadioButton 可选按钮 <%=Html.RadioButton("Name","value") %> RenderAction 呈现一个控制器行为方法 RenderPartial 由另一个视图引擎可选的呈现部分视图 RenderUserControl 呈现用户控件 RouteLink 生成特定的路由连接 SubmitButton 呈现一个类似按钮提交表单字段 SubmitImage 呈现一个类似图像提交表单字段 TextArea 呈现一个文本域 ValidationMessage 为特定的ViewData呈现一个验证信息 ValidationSummary 呈现一个ViewData的验证摘要。
 
原文:http://hi.baidu.com/xxl_cc/blog/item/a3719051eb62292843a75bb2.html
原文地址:https://www.cnblogs.com/xwj517537691/p/2495600.html