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<SelectListItem> selectList,    string optionLabel,    object htmlAttributes)
             name:选择元素的名称,绑定到元素视图的模型属性
             selectList:选项列表,
             optionLabel:类似与“Select …”   可选的默认的标签
             htmlAttributes:可添加的属性
             使用方法
            
 
        List<SelectListItem> items = new List<SelectListItem>();
         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('');'/>")%>
    编码结果
             &lt;script src=&quot;j . JS&quot;&gt;&lt;/script&gt;&lt;div background='javascript:alert('');'/&gt;
 
(6)Hidden 生成表单隐藏字段
            <%= Html.Hidden("SearchType", 1) %>
            前台源代码:
             <input id="SearchType" name="SearchType" type="hidden" value="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"   
             ) %>
 
             前台源代码:
                <select Length="16" id="optionsList" multiple="multiple" name="optionsList">
                      <option value="opt1">Option 1</option>
                      <option value="opt2">Option 2</option>
                      <option value="opt3">Option 3</option>
                </select>
(8)Pasword  生成表单密码字段
           使用方法:
                   <%= Html.Password("password", ViewData["password"]) %>
(9)TextBox 生成文本框
           使用方法:
               <%=Html.TextBox("Name", Model.Contact.Name)%>
           前台源代码:
                <input id="Name" name="Name" type="text" value="Alan" / >
(10)Button 生成一个按钮
             最新发布2.0.0.0MVC的已经没有了
(11)CheckBox  生成checkBox
          使用方法:
            <%= Html.CheckBox("a") %>
           前台源代码:
            <input id="a" name="a" type="checkbox"  />
(12)BeginForm  生成的HTML表单
         使用方法:
                    <% using (Html.BeginForm(Model.PostAction, "Home", FormMethod.Post))
                               { %>
        前台源代码:
                    <form action="/Home/Post" method="post">
(13)Image 生成图像标签
        引用一张图片
            <%=Html.Image("~/Images/bigwave.jpg") %><br />
         设置名称与宽度
            <%=Html.Image("~/Images/bigwave.jpg", "myWave", new { width = "30px" })%><br />
(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的验证摘要。

 

posted @ 2013-04-10 15:59  阳光小屋  阅读(220)  评论(0编辑  收藏  举报