2011-11-29 75 views
0

我在jQuery中为我的项目做了很多编码,但是我需要帮助获取由Helper with后缀生成的参数名称。MVC3 - DataAnnotation - propertys name

当我这样做:

Html.TextBoxFor(model => model.SomeTextParameter) 

在纯HTML还有的生成ID: “SomeTextParameter”。我想要在jQuery中使用它的每个地方自动生成它的id。怎么做?

或者,我可以随时manualy指定要使用的辅助无“对于”它的名字:

Html.TextBox("SomeTextParameter", this.Model.SomeTextParameter) 

在这种情况下,我可以控制,方便IDS,但是当我想使用数据注释的标签,然后我发现连接[Display(Name = "Really important text parameter")]Html.Label("SomeTextParameterLabel", <what to enter here?>)的另一个问题。

当我使用:

Html.LabelFor(model => model.SomeTextParameter) 

它结合,所以它显示,就像我想显示的名称。

任何想法如何解决第一或第二个问题?

回答

1
public class LogOnModel 
{ 
    [Required] 
    [Display(Name = "User name")] 
    public string UserName { get; set; } 

    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [Display(Name = "Remember me?")] 
    public bool RememberMe { get; set; } 
} 

<ol> 
    <li> 
     @Html.LabelFor(m => m.UserName) 
     @Html.TextBoxFor(m => m.UserName) 
     @Html.ValidationMessageFor(m => m.UserName) 
    </li> 
    <li> 
     @Html.LabelFor(m => m.Password) 
     @Html.PasswordFor(m => m.Password) 
     @Html.ValidationMessageFor(m => m.Password) 
    </li> 
    <li> 
     @Html.CheckBoxFor(m => m.RememberMe) 
     @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" }) 
    </li> 
</ol> 
得到生成的ID