主攻ASP.NET.3.5.MVC架构之重生:HtmlHelper(一)

 

HtmlHelper

 

FormExtensions静态类

 

BeginForm,BeginRouteForm,EndForm 三种类型

 

 

 

FormMethod.Post优先级最高

 

 

 

可以在多个地方设置action参数,htmlAttributes优先级别最高

 

 

 

BeginRouteForm扩展方法,提供开发者使用

 

 

 

设置路由值字典类型的id值和class

 

 

 

EndForm

 

<%Html.EndForm(); %>

 

 

 

代码

    <%using (Html.BeginForm("index", "home", FormMethod.Post, new { action = "/Controller/actionName" }))

      {%>

    表单内容

    <%}

    %>

    <%using (Html.BeginForm("index", "AfIndex",

          new RouteValueDictionary { { "id", 123 } },

          FormMethod.Post,

          new RouteValueDictionary { { "class", "cssName" } }

 

          ))

      {%>

    13重载方法

    <%}

    %>

    <%using (Html.BeginRouteForm(new { action = "action" }))

      {%>

    BeginRouteForm扩展方法,提供开发者使用

    <%}

    %>

    <%Html.EndForm(); %>

InputExtensions

CheckBox,Hidden,Password,RadioButton,TextBox五种类型

 

CheckBox

 

 

 

 

 

代码:

<%=Html.BeginForm("CheckBox","Home") %>

<fieldset>

<legend>设置字体:</legend>

 

<%=Html.CheckBox("MyCheckBox1",true,new{id="checkBox1"}) %>

<label for="checkBox1">黑体</label>

<%=Html.CheckBox("MyCheckBox2",false,new{id="checkBox2"}) %>

<label for="checkBox1">斜体</label>

<br/>

<input type="submit" value="Submit" />

</fieldset>

<%Html.EndForm(); %>

 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

 

<h2>CheckBox</h2>

<%=Html.TextBox("t1", ViewData["t1"])%>

<%=Html.TextBox("t2", ViewData["t2"])%>

</asp:Content>

 

public ActionResult CheckBox(FormCollection formcollection)

{

       //通过索引获得复选框控件状态值字符串

       bool myCheckBox1 = formcollection[0].Contains("true");

//通过键值获得复选框控件状态值字符串

       bool myCheckBox2 = formcollection["MyCheckBox2"].Contains("true");

 

       if (myCheckBox1)

              ViewData["t1"] = "1选中";

       else

              ViewData["t1"] = "1未选中";

       if (myCheckBox2)

              ViewData["t2"] = "2选中";

       else

              ViewData["t2"] = "2未选中";

       return View();

 

}

 

Hidden

<%=Html.Hidden("name",123) %>

 

 

Password

<%=Html.Password("password",123) %>

 

 

RadioButton

 

 

 

 

 

<% using (Html.BeginForm("RadioButton", "Home"))

   {%>

<fieldset>

       <legend>设置字号</legend>

       <%=Html.RadioButton("MyRadioButton","MyValue1",true,new{id="MyRadioButton1"})%>

       <label for="MyRadioButton1">

              10#</label>

       <%=Html.RadioButton("MyRadioButton","MyValue2",true,new{id="MyRadioButton2"})%>

       <label for="MyRadioButton1">

              20#</label>

       <input class="button" type="submit" value="" />

</fieldset>

<%} %>

 

<h2>RadioButton</h2>

<%=Html.TextBox("t1", ViewData["MyRadioButton"])%>

 

 

        public ActionResult RadioButton(FormCollection formcollection)

        {

            string radioButtion1=formcollection[0];

            string radioButtion2 = formcollection["MyRadioButton1"];

 

            ViewData["MyRadioButton"] = radioButtion1;

            return View();

        }

TextBox

 

<%=Html.TextBox("Message") %>

 

 

 

 

<%=Html.TextBox("myTextBox", null, new { size=50})%>

 

 

LinkExtensions

ActionLinkRouteLink两种类型

ActionLink

 

ActionLink扩展方法主要实现一个连接

 

<%=Html.ActionLink("actionlink", "About")%>

 

 

 

<%=Html.ActionLink("actionlink", "About","Home")%>

 

RouteLink

 

<%=Html.RouteLink("routLink", "default", new { id = 100 })%>

 

 

RenderPartialExtensions

 

RenderPartial该用户控件主要用来显示数据表Categories

ASP.NET MVC中使用RenderPartial方法时的一些性能问题,记住两点:一是在ASP.NET MVC应用程序发布到生产服务器时,别忘了关闭Debug模式(对于ASP.NET WebForm应用程序也是一样);二时尽可能的减少调用RenderPartial方法的次数,如通过在UserControl中进行遍历等方法。

 

原文地址:https://www.cnblogs.com/cube/p/2514170.html