HtmlHelper類的輔助和擴充方法:
(1)ActionLink 產生一個特定的控制器行為串連
<%=Html.ActionLink("Edit", "Edit", "Book", new { id = Model.ID }, new { @class = "BookDetail"})%>
具體說明:
Edit,為linkText,具體而言就是顯示的字串
Edit,對應為ActionName;
Book,為Controller;
new { id = Model.ID },為產生元素的id定義;
new { @class = “BookDetail” },則為元素添加了tag要素
(2)AntiForgeryToken 產生一個隱藏的表單欄位,可與ValidateAntiForgeryToken屬性一起使用以確保Request沒有被修改
具體說明:
如果你要保護你的應用程式的多個形式的相互獨立,AntiForgeryToken 將產生不同的防偽標誌
<%= Html.AntiForgeryToken("someArbitraryString") %>
[ValidateAntiForgeryToken(Salt="someArbitraryString")]
public ViewResult SubmitUpdate()
{
// ... etc
}
(3)AttributeEncode 資料屬性編碼 :提供了HtmlAttributeEncode功能
具體說明:
<%= Html.AttributeEncode(Url.Action("Login")) %>
(4)DropDownList 基於一對索引值對產生DropDownList
具體說明:
Html.DropDownList( string name, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes)
name:選擇元素的名稱,綁定到元素視圖的模型屬性
selectList:選項列表,
optionLabel:類似與“Select …” 可選的預設的標籤
htmlAttributes:可添加的屬性
使用方法
代碼
List<SelectListItem> items = new List<SelectListItem>();
items.Add( new SelectListItem { Text = "Swimming", Value = "1" });
items.Add(new SelectListItem { Text = "Cycling", Value = "2", Selected = true });
items.Add(new SelectListItem { Text = "Running", Value = "3" });
ViewData["ListItems"] = items;
<%= Html.DropDownList("ListItems") %>
(5)Encode 編碼字串,以防跨站指令碼攻擊
使用方法
< %=Html.Encode("< script src=\"j . JS\">< div background='javascript:alert('');'/>")%>
編碼結果
<script src="j . JS"></script><div background='javascript:alert('');'/>
(6)Hidden 產生表單隱藏欄位
<%= Html.Hidden("SearchType", 1) %>
前台原始碼:
<input id="SearchType" name="SearchType" type="hidden" value="1" />
(7)ListBox 基於列索引值對產生下拉框
使用方法:
代碼
<%= Html.ListBox(
"optionsList",
new []
{
new SelectListItem
{
Text = "Option 1",
Value = "opt1"
},
new SelectListItem
{
Text = "Option 2",
Value = "opt2"
},
new SelectListItem
{
Text = "Option 3",
Value = "opt3"
}
},
"Choose an option"
) %>
前台原始碼:
<select Length="16" id="optionsList" multiple="multiple" name="optionsList">
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
<option value="opt3">Option 3</option>
</select>
(8)Pasword 產生表單密碼欄位
使用方法:
<%= Html.Password("password", ViewData["password"]) %>
(9)TextBox 產生文字框
使用方法:
<%=Html.TextBox("Name", Model.Contact.Name)%>
前台原始碼:
<input id="Name" name="Name" type="text" value="Alan" / >
(10)Button 產生一個按鈕
最新發行2.0.0.0MVC的已經沒有了
(11)CheckBox 產生checkBox
使用方法:
<%= Html.CheckBox("a") %>
前台原始碼:
<input id="a" name="a" type="checkbox" />
(12)BeginForm 產生的HTML表單
使用方法:
<% using (Html.BeginForm(Model.PostAction, "Home", FormMethod.Post))
{ %>
前台原始碼:
<form action="/Home/Post" method="post">
(13)Image 產生映像標籤
引用一張圖片
<%=Html.Image("~/Images/bigwave.jpg") %><br />
設定名稱與寬度
<%=Html.Image("~/Images/bigwave.jpg", "myWave", new { width = "30px" })%><br />
(14)Mailto 產生Email串連
Simple Email:
<%=Html.MailTo("mailto:weflytotti@163.com%22,%22Email Me!") %>
With Subject and Body
<%=Html.MailTo("mailto:weflytotti@163.com%22,%22Email Me!","Sending you a note"
,"Hey - wanted to say hi!") %>
(15)RadioButton 可選按鈕
<%=Html.RadioButton("Name","value") %>
RenderAction 呈現一個控制器行為方法
RenderPartial 由另一個視圖引擎可選的呈現部分視圖
RenderUserControl 呈現使用者控制項
RouteLink 產生特定的路由串連
SubmitButton 呈現一個類似按鈕提交表單欄位
SubmitImage 呈現一個類似映像提交表單欄位
TextArea 呈現一個文本域
ValidationMessage 為特定的ViewData呈現一個驗證資訊
ValidationSummary 呈現一個ViewData的驗證摘要。