[MVC HtmlHelper简单了解]

HtmlHelper用来在视图中显示Html控件,简化代码,使用方便!,降低了View视图中的代码复杂度!可以更快速的完成工作!

  

以下是一些常用 的html标签 辅助方法

 使用HTML辅助方法输出  kBeginForm  

//视图代码
@using (Html.BeginForm("search", "home", FormMethod.Get),new { target="_black",@class="form1" })
{ 
    <input type="text" value="" />
}
//生成的HTML代码
<form action="/home/search" class="form1" method="get" target="_black">
  <input type="text" value="" />
</form>

使用HTML辅助方法输出超链接 ActionLink  RouteLink

1.@Html.ActionLink("链接文字")

   <a href="/">这是一个连接</a>

2.@Html.ActionLink("链接文字","ActionName")

   <a href="ControllerName/ActionName">这是一个连接</a>    //当前页的控制器所在页

3. @Html.ActionLink("链接文字","ActionName","ControllerName")

   <a href="/ControllerName/ActionName">链接文字</a>

4. @Html.ActionLink("链接文字", "ActionName","ControllerName" new { page=1 })

   <a href="/ControllerName/ActionName/??page=1">链接文字</a>

5.@Html.ActionLink("连接文字", "ActionName", "ControllerName", new { page = 1 }, new { id = "linkID" })

  <a href="/ControllerName/ActionName/??page=1" id="linkID">链接文字</a> 


 提示: RouteLink 跟 ActionLink 在功能上一样。所以不一一概况

  使用HTML辅助方法输出文本框 TextBox

1.@Html.TextBox("文本名称")

<input id="文本名称" name="文本名称" type="text" value="" />

2.@Html.TextBox("input2",Model.CategoryName,new{ @style = "300px;" })

<input id="文本名称" name="文本名称" style="300px;"  type="text" value="@Model.CategoryName" /> 

3.@Html.TextBox("文本名称", ViewData["Name"],new{ @style = "300px;" })

<input id="input3" name="input3" style="300px;" type="text" value="@ViewData["Name"]" />

  使用HTML辅助方法输出文本域 TextArea 

@Html.TextArea("文本名称", Model.CategoryName, 3, 9,null)
<textarea  id="文本名称" name="文本名称" rows="3" cols="9" >@Model.CategoryName</textarea>

   使用HTML辅助方法输出多选按钮 Checkbox

1.@Html.CheckBox("名称",true) 
 <input checked="checked" id="名称" name="名称" type="checkbox" value="true" /><input name="名称" type="hidden" value="false" />

2.@Html.CheckBox("名称", new { @class="checkBox"})
<input class="checkBox" id="名称" name="名称" type="checkbox" value="true" /><input name="名称" type="hidden" value="false" />

     使用HTML辅助方法输出单选按钮 RadioButton 

1. @Html.RadioButton("名称", "",false )

<input id="名称" name="名称" type="radio" value="男"/>

2.@Html.RadioButton("名称", "男",true)

<input id="名称" name="名称" type="radio" value="男"checked="checked" />

   使用HTML辅助方法输出隐藏按钮  Hidden

@Html.Hidden("名称","文本",new{}); 

<input id="名称" name="名称" type="hidden" value="文本" />;

 使用HTML辅助方法输出多选按钮  Password  

@Html.Password("名称", 文本, new { @class="class1" })

<input class="class1" id="名称" name="名称" type="password" value="文本" /

 使用HTML辅助方法输出 label

@Html.Label("label1","你好")

<label for="label1">你好</label>

 使用HTML辅助方法输出 DropDownList  

//视图代码
  @{ 
        List<SelectListItem> list = new List<SelectListItem> {

            new SelectListItem { Text = "启用", Value = "0",Selected = true},

            new SelectListItem { Text = "禁用", Value = "1" } 
        };
 }
 @Html.DropDownList("state",list,null,new{})

  //生成的Html代码

  <select id="state" name="state">
    <option selected="selected" value="0">启用</option> 
    <option value="1">禁用</option> 
  </select>

  

不要总是说的很多,却做得很少! 要明白'生于忧患,死于安乐'!
原文地址:https://www.cnblogs.com/shaopang/p/6908300.html