c# - 更改 "for"中的属性 "Html.LabelFor"

标签 c# .net asp.net-mvc razor html-helper

我需要更改 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/

相关文章:

c# - visual studio 中跳过的测试是什么?

c# - 为{0}增值

c# - MySQL选择结果保存到c#变量中

.net - 使用 .NET XLinq 解析 svg - 'xlink' 是未声明的前缀

.net - 用于从/向数据库检索/发送数据的 API

asp.net-mvc - 如何在验证摘要中添加图像以及错误消息?

c# - MVC 中每个请求的带有 Owin DbContext 的 Entity Framework

C# 表达式调用,似乎无法获取正确的方法信息和参数

c# - 字符到数字的转换过程失败

c# - 如何处理我们没有引用的一次性元素?