mvc中的htmlhelper控件类

1.FormExtensions类
该类扩展了HtmlHelper类,主要用于提供HTML标签<form>的定义。主要方法如下:
BegionForm()
BegionRouteForm()
EndForm()
示例1:BeginForm的演示
<% Html.BeginForm(); %>
输出:<form action="/default.aspx?" method="post"> //默认post方法

<% Html.BeginForm(
"ActionName",
"ControllerName",
new RouteValueDictionary { { "id", 123 } },
FormMethod.Post,
new RouteValueDictionary { { "class", "cssName" } }); %>
输出:<form action="/ControllerName/ActionName/123" class="cssName" method="post">

<% Html.BeginForm(
"ActionName",
"ControllerName",
FormMethod.Post,
new { method="Get" } ); %>
输出: <form action="/ControllerName/ActionName" method="post"> //FormMethod.Post的优先级最高

示例2:BegionRouteForm的演示
<% Html.BeginRouteForm("Default"); %>
输出:<form action="/" method="post">

<% Html.BeginRouteForm(new { action="action" }); %>
输出:<form action="/Home/action" method="post">

<% Html.BeginRouteForm(new {controller="controller",action="action" }); %>
输出:<form action="/controller/action" method="post">

示例3:EndForm的演示
<% Html.EndForm(); %>
输出:</form>

2.InputExtensions类
该类主要提供5中类型的Input控件,结构如下:

示例1: 演示TextBox控件
<% =Html.TextBox("txtName") %>
输出:<input id="txtName" name="txtName" type="text" value="" />

<% =Html.TextBox("Message") %>
输出:<input id="Message" name="Message" type="text" value="向页面传递数据啦!" />
value的值是通过ViewData["Message"]默认得到的。

<% =Html.TextBox("myTextBox",null, new { size=50 })%>
输出:input id="myTextBox" name="myTextBox" size="50" type="text" value="" />


示例2: 演示Password控件
<% =Html.Password ("MyPassword") %>
输出: <input id="MyPassword" name="MyPassword" type="password" />

<% =Html.Password("Password", 123) %>
输出: <input id="Password" name="Password" type="password" value="123" />

示例3: 演示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="checkBox2">斜体</label>
<input type="submit" value="CheckBox" />
</fieldset>
<% Html.EndForm(); %>
Controller部分
public ActionResult CheckBox(FormCollection formCollection)
{
bool myCheckBox1=formCollection[0].Contains ("true");
bool myCheckBox2 = formCollection["MyCheckBox2"].Contains("true");

ViewData["checkBox1"] = myCheckBox1;
ViewData["checkBox2"] = myCheckBox2;
return View();
}
输出:
<form action="/Home/CheckBox" method="post">
<fieldset>
<legend>设置字体:</legend>
<input checked="checked" id="checkBox1" name="MyCheckBox1" type="checkbox" value="true" /><input name="MyCheckBox1" type="hidden" value="false" />
<label for="checkBox1">黑体</label>
<input id="checkBox2" name="MyCheckBox2" type="checkbox" value="true" /><input name="MyCheckBox2" type="hidden" value="false" />
<label for="checkBox2">斜体</label>

<input type="submit" value="CheckBox" />
</fieldset>
</form>

示例4: 演示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", new { id = "MyRadioButton2" })%>
<label for="MyRadioButton2">20号</label>
<input type="submit" value="RadioButton" />
</fieldset>
<% } %>
Controller部分:
public ActionResult RadioButton(FormCollection formCollection)
{
string radioButton1 = formCollection[0];
string radioButton2 = formCollection["MyRadioButton"];

ViewData["MyRadioButton"] = radioButton1 ;
return View();
}
输出:
<form action="/Home/RadioButton" method="post">
<fieldset>
<legend>设置字号:</legend>
<input checked="checked" id="MyRadioButton1" name="MyRadioButton" type="radio" value="MyValue1" />
<label for="MyRadioButton1">10号</label>
<input id="MyRadioButton2" name="MyRadioButton" type="radio" value="MyValue2" />
<label for="MyRadioButton2">20号</label>
<input type="submit" value="RadioButton" />
</fieldset>
</form>

示例5:Hidden控件
<% =Html.Hidden("testName") %>
输出:<input id="testName" name="testName" type="hidden" value="" />

<% =Html.Hidden("testName", 123) %>
输出:<input id="testName" name="testName" type="hidden" value="123" />

3.LinkExtensions类
该类可以在页面生成一些<a>标记,该类有2个重要的方法:
ActionLink(string linkText,string actionName,string controllerName)
RouteLink(string linkText,string routeName,object routeValue)
示例:
<% =Html.ActionLink("演示程序","List") %> //<a href="/Home/List">演示程序</a>
<% =Html.ActionLink ("testLink","actionName") %> //<a href="/Home/actionName">testLink</a>
<% =Html.ActionLink ("testLink","actionName","controller") %> //<a href="/controller/actionName">testLink</a>
<% =Html.RouteLink("routLink",new { controller="controller",action="action"} ) %> //<a href="/controller/action">routLink</a>
<% =Html.RouteLink("routLink","default",new { id=100 } ) %> //<a href="/Home/Index/100">routLink</a>

4.SelectExtensions类
该类主要用于在页面提供DropDownList控件和ListBox控件,如:
示例1: 演示DropDownList控件
<% Html.BeginForm("DropDownList", "Home"); %>
<fieldset>
<legend>选择产品目录:</legend>
<%= Html.DropDownList("CategoryID") %>
<input type="submit" value="DropDownList" />
</fieldset>
<% Html.EndForm(); %>
Controller部分如下:
public ActionResult DropDownList(FormCollection formCollection)
{
string dropDownList1 = formCollection[0];
string dropDownList2 = formCollection["CategoryID"];

TestDataContextDataContext db = new TestDataContextDataContext();
ViewData["CategoryID"] = new SelectList(db.Categories,"CategoryName","CategoryName", dropDownList1);
return View();
}

输出:
<form action="/Home/DropDownList" method="post">
<fieldset>
<legend>选择产品目录:</legend>
<select id="CategoryID" name="CategoryID"><option value="Beverages">Beverages</option>
<option value="Condiments">Condiments</option>
<option value="Confections">Confections</option>
<option value="Dairy Products">Dairy Products</option>
</select>
<input type="submit" value="DropDownList" />
</fieldset>
</form>

示例2:演示ListBox控件
<% Html.BeginForm("ListBox", "Home"); %>
<fieldset>
<legend>选择产品目录:</legend>
<%= Html.ListBox ("CategoryID") %>
<input type="submit" value="ListBox" />
</fieldset>
<% Html.EndForm(); %>
Controller部分:
public ActionResult ListBox(FormCollection formCollection)
{
var dropDownList1 = formCollection[0].Split(',').AsEnumerable() ;
var dropDownList2 = formCollection["CategoryID"].Split(',').AsEnumerable () ;

TestDataContextDataContext db = new TestDataContextDataContext();
ViewData["CategoryID"] = new MultiSelectList (db.Categories, "CategoryName", "CategoryName", dropDownList1);
return View();
}
输出:
<form action="/Home/ListBox" method="post">
<fieldset>
<legend>选择产品目录:</legend>
<select id="CategoryID" multiple="multiple" name="CategoryID">
<option value="Beverages">Beverages</option>
<option value="Condiments">Condiments</option>
<option value="Confections">Confections</option>
</select>
<input type="submit" value="ListBox" />
</fieldset>
</form>

5.RenderPartialExtensions类
该类主要用于在视图中显示用户控件,便于视图页面的封装。其结构如下:
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName);
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model);
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, ViewDataDictionary viewData);
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model, ViewDataDictionary viewData);

例1:
加载同一目录下的List.ascx用户控件代码如下:
<% Html.RenderPartial("List");%>

6.TextAreaExtensions类
该类主要用于在页面上生成TextArea控件,其结构如下:
public static string TextArea(this HtmlHelper htmlHelper, string name);
public static string TextArea(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes);
...
例1:
<% =Html.TextArea("textArea",new { rows=5,cols=10}) %>
输出:<textarea id="textArea" name="textArea" cols="10" rows="5"></textarea>

例2:
<% =Html.TextArea("Message") %> //默认cols=20 rows=2
输出:<textarea cols="20" id="Message" name="Message" rows="2">ASP.NET MVC HTML控件使用解析!</textarea>

例3:
<% =Html.TextArea ("textArea","Test data") %>
输出: <textarea cols="20" id="textArea" name="textArea" rows="2">Test data</textarea>

例4:
<% =Html.TextArea ("textArea","Test data",10,8,null) %>
输出:<textarea cols="8" id="textArea" name="textArea" rows="10">Test data</textarea>


7.ValidationExtensions类
该类用于实现相关控件的输入验证,其中主要有2种控件:验证控件为ValidationMessage控件和ValidationSummary控件,其结构如下:
(1).ValidationMessage验证信息控件
ValidationMessage(this HtmlHelper html,string modelName); 有6个重载方法
使用如下:
<%= Html.TextBox("email") %>
<%= Html.ValidationMessage("email","*") %>
上述代码在email文本框输入空白信息时,就用*号提示

(2).ValidationSummary信息摘要控件
ValidationSummary(this HtmlHelper html,string message); 有4个重载方法,该控件一般用在表单外部。
使用如下:
<%= Html.ValidationSummary("Account creation was unsuccessful. Please correct the errors and try again.") %>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值