主攻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(); %>
<: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中进行遍历等方法。
摘自 小念头-cube.net
下一篇: 我国乡镇企业家的精神生活写实