Asp.net MVC中关于@Html标签的使用

@Html帮助器简单说明,记录些基本的跟HTML中对应的@html帮助器,@Html基本包含了html中的表单控件和常用Html
在@Html中,带有For的主要是针对强类型的Html类型。

用于说明@Html中标签,定义Student对象用于辅助说明,

[html] view plain copy
  1. public class Student  
  2. {  
  3.   /// <summary>  
  4.         /// 姓名  
  5.         /// </summary>  
  6.         ///   
  7.         [DisplayName("姓名")]  
  8.         public string Name  
  9.         {  
  10.             get;  
  11.             set;  
  12.         }  
  13.   
  14.   
  15.         /// <summary>  
  16.         /// 年龄  
  17.         /// </summary>  
  18.         public int Age  
  19.         {  
  20.             get;  
  21.             set;  
  22.         }  
  23. }  


在cshtml页面定义@model Student
Label标签,在LabelExtensions类中实现
1、 @Html.Label()
返回一个 HTML label 元素和由指定表达式表示的属性的属性名称。
参数:string expression,string labelText
expression:一个表达式表示要显示的属性
labelText:显示文字
例: 

[html] view plain copy
  1. @Html.Label("weight")       输出:<label for="weight">weight</label>  
  2. abel("name","姓名")  输出:<label for="name">姓名</label>  


2、 @Html.LabelFor()
和@Html.Label()类似,只是主要针对强类型
例:

[html] view plain copy
  1.        @Html.LabelFor(model=>mode.Name) 输出:  <label for="Name">Name</label>  
  2. @Html.LabelFor(model=>mode.Name,"姓名") 输出:<label for="name">姓名</label>  
  3. 如果在上面实体添加[DisplayName("姓名")]特性(引用System.ComponentModel;)则会显示:  
  4. <label for="Name">姓名</label>用于显示汉字很方便  


3、 @Html.LabelForModel()
例:@Html.LabelForModel("name") 输出: <label for="">name</label>