主攻ASP.NET.3.5.MVC架构之重生:HtmlHelper(一)
2012-05-23 08:12
267 查看
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类
ActionLink,RouteLink两种类型
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中进行遍历等方法。
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类
ActionLink,RouteLink两种类型
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中进行遍历等方法。
相关文章推荐
- 主攻ASP.NET.3.5.MVC架构之重生:HtmlHelper(二)
- 主攻ASP.NET.3.5.MVC3.0架构之重生:自定义农历日期天气格式Helper
- 主攻ASP.NET.3.5.MVC架构之重生: URL Routing (三)
- 主攻ASP.NET.3.5.MVC架构之重生: 控制器与视图之间的值传递(四)
- 主攻ASP.NET.3.5.MVC架构之重生: LINQ(五)
- 主攻ASP.NET.3.5.MVC架构之重生:Controller(七)
- 主攻ASP.NET.3.5.MVC架构之重生: LINQ(六)
- 主攻ASP.NET.3.5.MVC架构之重生:省、市,区三级联动(八)
- 主攻ASP.NET.3.5.MVC3.0架构之重生:文件上传(十二)
- 主攻ASP.NET.4.5.1 MVC5.0之重生:创建UIHelper通用自定义分页和选择开关与PagesHelper和IsSelect简单用法
- 主攻ASP.NET.3.5.MVC3.0架构之重生:用户角色与用户增删改查(十)
- 主攻ASP.NET.3.5.MVC3.0架构之重生:Filter(九)
- 主攻ASP.NET.3.5.MVC3.0架构之重生:CheckBox(十一)
- 主攻ASP.NET.3.5.MVC3.0架构之重生:MVC在Global.asax.定义定时处理程序
- ASP.NET 3.5 MVC 架构与实战笔记6 HtmlHelper控件解析
- ASP.NET 3.5 MVC 架构与实战笔记6 HtmlHelper控件解析
- ASP.NET MVC的帮助类HtmlHelper和UrlHelper
- 在 ASP.NET MVC 中创建自定义 HtmlHelper
- 我对ASP.NET MVC HtmlHelper中的Form和Link的小扩展
- ASP.NET MVC HtmlHelper用法集锦