我需要更改 id
我模型的属性,所以我分配一个新的 id
在方法中 TextBoxFor
来自 HTML helper
.但这显然没有改变id
。在for
使用方法时的属性 LabelFor
来自 HTML helper
.
@Html.TextBoxFor(model => model.MyProperty, new { id = "CustomId" })
如何更改 for
使用方法时的属性 LabelFor
来自 HTML helper
?。因为此方法不允许更改属性。
@Html.LabelFor(model => model.MyProperty)
也许有一个属性可以改变id
在模型属性中。
谢谢
编辑评论
我使用 LabelFor
因为我需要从 DataAnnotation
中取名字Description
:
[Display(Name = "Name of my property")]
public string MyProperty { get; set; }
最佳答案
View 模型:
public IEnumerable<SportingLisbon> SportingLisbonList { get; set; }
查看:
@Html.LabelFor(model => model.SportingLisbonList, new { @for = "IdSportingLisbon" })
浏览器:
描述:
在 LabelFor 上设置不同的 for 属性。
没有 new { @for = "IdSportingLisbon" }
浏览器:
网络配置:
<compilation debug="true" targetFramework="4.5" />
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
关于c# - 更改 "for"中的属性 "Html.LabelFor",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11759423/