Html.LabelFor

数据实体

 

[c-sharp] view plain copy print ?
  1. public class LoginModel  
  2.     {  
  3.         public LoginModel()  
  4.         {  
  5.             LoginName = HttpContext.Current.User.Identity.Name;  
  6.         }  
  7.   
  8.         [Required]  
  9.         [DisplayName("用户名:")]  
  10.         public string LoginName   
  11.         { getset; }  
  12.   
  13.         [Required]  
  14.         [DataType(DataType.Password)]  
  15.         [DisplayName("密码:")]  
  16.         public string Password   
  17.         { getset; }  
  18.   
  19.         /// <summary>  
  20.         /// 用户验证结果  
  21.         /// </summary>  
  22.         public string ValidateResult  
  23.         { getset; }  
  24.     }  
public class LoginModel { public LoginModel() { LoginName = HttpContext.Current.User.Identity.Name; } [Required] [DisplayName("用户名:")] public string LoginName { get; set; } [Required] [DataType(DataType.Password)] [DisplayName("密码:")] public string Password { get; set; } /// <summary> /// 用户验证结果 /// </summary> public string ValidateResult { get; set; } }

 

页面上使用Html.LabelFor:


[c-sharp] view plain copy print ?
  1. <table width="460" border="0" align="center" >     
  2.   
  3.           <tr>  
  4.                  <td>  
  5.                         
  6.                  </td>  
  7.                  <td width="52">  
  8.                      <%: Html.LabelFor(m => m.LoginName) %>  
  9.                  </td>  
  10.                  <td width="187">  
  11.                      <%: Html.TextBoxFor(m => m.LoginName) %>  
  12.                  </td>  
  13.              </tr>  
  14.              <tr>  
  15.                  <td>  
  16.                         
  17.                  </td>  
  18.                  <td>  
  19.                      <%: Html.LabelFor(m => m.Password) %>  
  20.                  </td>  
  21.                  <td>  
  22.                      <%: Html.TextBoxFor(m => m.Password) %>  
  23.                  </td>  
  24.              </tr>  
  25.              <tr>  
  26.                  <td>  
  27.                         
  28.                  </td>  
  29.                  <td>  
  30.                         
  31.                  </td>  
  32.                  <td>  
  33.                      <input type="submit" value="登 陆" style="width: 60px; background: url(../../../Content/Images/Button/btn57-21.jpg) no-repeat;  
  34.                          border: none; color: #000;" />     
  35.                      <input type="reset" value="取 消" style="width: 60px; background: url(../../../Content/Images/Button/btn57-21.jpg) no-repeat;  
  36.                          border: none; color: #000;" />  
  37.                  </td>  
  38.              </tr>  
  39. ;/table>  
<table width="460" border="0" align="center" > <tr> <td>   </td> <td width="52"> <%: Html.LabelFor(m => m.LoginName) %> </td> <td width="187"> <%: Html.TextBoxFor(m => m.LoginName) %> </td> </tr> <tr> <td>   </td> <td> <%: Html.LabelFor(m => m.Password) %> </td> <td> <%: Html.TextBoxFor(m => m.Password) %> </td> </tr> <tr> <td>   </td> <td>   </td> <td> <input type="submit" value="登 陆" style="width: 60px; background: url(../../../Content/Images/Button/btn57-21.jpg) no-repeat; border: none; color: #000;" />    <input type="reset" value="取 消" style="width: 60px; background: url(../../../Content/Images/Button/btn57-21.jpg) no-repeat; border: none; color: #000;" /> </td> </tr> </table>

 

 

运行效果:

Html.LabelFor_第1张图片

LabelFor使用了页面的DisplayName属性

你可能感兴趣的:(Html.LabelFor)