在我的用户表单中,我想使用 html.Labelfor 帮助器,但我想要的是类别名称,而不是 CatName 的模型属性名称。我知道我可以将其放入 a 中,但我必须一遍又一遍地重复使用这些名称。下面是示例。
模型属性:
public string CatName { get; set; }
查看:
@Html.LabelFor(model => model.CatName) //Displays CatName , I want Category Name
最佳答案
您可以使用DataAnnotations
[Display(Name = "Category Name")]
public string CatName { get; set; }
这是首选方式。
您还可以使用 LabelFor
的重载,它采用字符串作为标签文本
@Html.LabelFor(model => model.CatName, "Category Name")
关于asp.net-mvc - 在 MVC 中,有没有一种方法可以对模型属性进行别名以在 html.Labelfor 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13087424/