使用数据注释来定义哪些属性应该用作DropDown文本?
问题描述:
我有以下型号:使用数据注释来定义哪些属性应该用作DropDown文本?
public class Bus
{
public int ID { get; set; }
[Required]
public string From { get; set; }
[Required]
public string To { get; set; }
[Required]
public string Label { get; set; }
}
当我这样做:
@Html.DropDownList("BusID", null, htmlAttributes: new { @class = "form-control" })
下拉文本标签是 '从' 属性。
我想知道是否有任何方式通过数据注释来定义在渲染下拉列表时将哪个字段用作“文本”?
谢谢!
答
您不能通过数据注释定义使用的属性,但您可以使用DropDownListFor-Method来完成此操作,就像我在我的评论中提到的一样。
看看这个问题:[如何选择显示的文本](https://stackoverflow.com/questions/28063846/how-to-choose-displayed-text-on-html-dropdownlistfor) – Nikolaus
您是否设法解决你的问题? – Nikolaus