MVC HtmlHelper guide

HtmlHelper用来在视图中呈现 HTML 控件。

以下列表显示了当前可用的一些 HTML 帮助器。 本主题演示所列出的带有星号 (*) 的帮助器。

 

  • ActionLink - 链接到操作方法。

  • BeginForm * - 标记窗体的开头并链接到呈现该窗体的操作方法。

  • CheckBox * - 呈现复选框。

  • DropDownList * - 呈现下拉列表。

  • Hidden - 在窗体中嵌入未呈现的信息以供用户查看。

  • ListBox * - 呈现列表框。

  • Password - 呈现用于输入密码的文本框。

  • RadioButton * - 呈现单选按钮。

  • TextArea - 呈现文本区域(多行文本框)。

 

TextBox * — Renders a text box." color="#000000">1.ActionLink

TextBox * — Renders a text box." color="#000000"> 复制代码
TextBox * — Renders a text box." color="#000000">@Html.ActionLink("这是一个连接", "Index", "Home")
带有QueryString的写法
@Html.ActionLink("这是一个连接", "Index", "Home", new { page=1 },null)
@Html.ActionLink("这是一个连接", "Index", new { page=1 })
有其它Html属性的写法
@Html.ActionLink("这是一个连接", "Index", "Home", new { id="link1" })
@Html.ActionLink("这是一个连接", "Index",null, new { id="link1" })
QueryString与Html属性同时存在
@Html.ActionLink("这是一个连接", "Index", "Home", new { page = 1 }, new { id = "link1" })
@Html.ActionLink("这是一个连接", "Index" , new { page = 1 }, new { id = "link1" })
TextBox * — Renders a text box." color="#000000"> 复制代码

TextBox * — Renders a text box." color="#000000">生成结果为:

TextBox * — Renders a text box." color="#000000"> 复制代码
TextBox * — Renders a text box." color="#000000"><a href="/">这是一个连接</a>
带有QueryString的写法
<a href="/?page=1">这是一个连接</a>
<a href="/?page=1">这是一个连接</a>
有其它Html属性的写法
<a href="/?Length=4" id="link1">这是一个连接</a>
<a href="/" id="link1">这是一个连接</a>
QueryString与Html属性同时存在
<a href="/?page=1" id="link1">这是一个连接</a>
<a href="/?page=1" id="link1">这是一个连接</a>
TextBox * — Renders a text box." color="#000000"> 复制代码
TextBox * — Renders a text box." color="#000000">2.RouteLink
跟ActionLink在功能上一样。
TextBox * — Renders a text box." color="#000000">@Html.RouteLink("关于", "about", new { })
带QueryString
@Html.RouteLink("关于", "about", new { page = 1 })
@Html.RouteLink("关于", "about", new { page = 1 }, new { id = "link1" })

TextBox * — Renders a text box." color="#000000"> 

TextBox * — Renders a text box." color="#000000">生成结果:

TextBox * — Renders a text box." color="#000000"><a href="/about">关于</a>
<a href="/about?page=1">关于</a>
<a href="/about?page=1" id="link1">关于</a>

TextBox * — Renders a text box." color="#000000">3.Form   2种方法

TextBox * — Renders a text box." color="#000000">@using(Html.BeginForm("index","home",FormMethod.Post)){

}
Or
@Html.BeginForm("index", "home", FormMethod.Post)
@Html.EndForm()

TextBox * — Renders a text box." color="#000000">生成结果:
<form action="/home/index" method="post"></form>

TextBox * — Renders a text box." color="#000000">
4.TextBox , Hidden ,

TextBox * — Renders a text box." color="#000000">@Html.TextBox("input1") 
@Html.TextBox("input2",Model.CategoryName,new{ @style = "width:300px;" }) 
@Html.TextBox("input3", ViewData["Name"],new{ @style = "width:300px;" }) 
@Html.TextBoxFor(a => a.CategoryName, new { @style = "width:300px;" })

TextBox * — Renders a text box." color="#000000"> 

TextBox * — Renders a text box." color="#000000">生成结果:

TextBox * — Renders a text box." color="#000000"> 
<input id="input1" name="input1" type="text" value="" />
<input id="input2" name="input2" style="width:300px;" type="text" value="Beverages" />
<input id="input3" name="input3" style="width:300px;" type="text" value="" />
<input id="CategoryName" name="CategoryName" style="width:300px;" type="text" value="Beverages" />

TextBox * — Renders a text box." color="#000000"> 

TextBox * — Renders a text box." color="#000000">5.TextArea

TextBox * — Renders a text box." color="#000000">@Html.TextArea("input5", Model.CategoryName, 3, 9,null)
@Html.TextAreaFor(a => a.CategoryName, 3, 3, null)

TextBox * — Renders a text box." color="#000000"> 

TextBox * — Renders a text box." color="#000000">生成结果:

TextBox * — Renders a text box." color="#000000"><textarea cols="9" id="input5" name="input5" rows="3">Beverages</textarea>
<textarea cols="3" id="CategoryName" name="CategoryName" rows="3">Beverages</textarea>

TextBox * — Renders a text box." color="#000000">
6.CheckBox

TextBox * — Renders a text box." color="#000000">@Html.CheckBox("chk1",true) 
@Html.CheckBox("chk1", new { @class="checkBox"}) 
@Html.CheckBoxFor(a =>a.IsVaild, new { @class = "checkBox" })

TextBox * — Renders a text box." color="#000000"> 

TextBox * — Renders a text box." color="#000000">生成结果:

TextBox * — Renders a text box." color="#000000"> 复制代码
TextBox * — Renders a text box." color="#000000"> 
<input checked="checked" id="chk1" name="chk1" type="checkbox" value="true" /><input name="chk1" type="hidden" value="false" />
 
<input class="checkBox" id="chk1" name="chk1" type="checkbox" value="true" /><input name="chk1" type="hidden" value="false" />
 
<input checked="checked" class="checkBox" id="IsVaild" name="IsVaild" type="checkbox" value="true" /><input name="IsVaild" type="hidden" value="false" />
TextBox * — Renders a text box." color="#000000"> 复制代码

TextBox * — Renders a text box." color="#000000"> 

TextBox * — Renders a text box." color="#000000">7.ListBox

TextBox * — Renders a text box." color="#000000">@Html.ListBox("lstBox1",(SelectList)ViewData["Categories"])
@Html.ListBoxFor(a => a.CategoryName, (SelectList)ViewData["Categories"])

TextBox * — Renders a text box." color="#000000"> 

TextBox * — Renders a text box." color="#000000">
生成结果:

TextBox * — Renders a text box." color="#000000"> 复制代码
TextBox * — Renders a text box." color="#000000"><select id="lstBox1" multiple="multiple" name="lstBox1">
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option selected="selected" value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>
<select id="CategoryName" multiple="multiple" name="CategoryName">
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>
TextBox * — Renders a text box." color="#000000"> 复制代码

TextBox * — Renders a text box." color="#000000">
8.DropDownList

TextBox * — Renders a text box." color="#000000">@ Html.DropDownList("ddl1", (SelectList)ViewData["Categories"],  "--Select One--")
@Html.DropDownListFor(a => a.CategoryName, (SelectList)ViewData["Categories"], "--Select One--", new { @class = "dropdownlist" })

TextBox * — Renders a text box." color="#000000"> 
生成结果:

TextBox * — Renders a text box." color="#000000"> 复制代码
TextBox * — Renders a text box." color="#000000"><select id="ddl1" name="ddl1">
<option value="">--Select One--</option>
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option selected="selected" value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>
<select class="dropdownlist" id="CategoryName" name="CategoryName">
<option value="">--Select One--</option>
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>
TextBox * — Renders a text box." color="#000000"> 复制代码

TextBox * — Renders a text box." color="#000000"> 

TextBox * — Renders a text box." color="#000000">9.Partial 视图模板
类似于webform里的自定义控件。

TextBox * — Renders a text box." color="#000000">@Html.RenderPartial("DinnerForm")  

TextBox * — Renders a text box." color="#000000"> 

TextBox * — Renders a text box." color="#000000">
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值