使用Data Annotations定义哪个属性应该用作DropDown Text?

时间:2017-10-15 20:49:12

标签: entity-framework model-view-controller data-annotations

我有以下型号:

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" })

下拉文本标签是'From'属性。

我想知道是否有任何方法可以通过数据注释来定义在呈现下拉列表时将哪个字段用作“文本”?

谢谢!

1 个答案:

答案 0 :(得分:0)

您无法通过数据注释定义使用的属性,但您可以使用DropDownListFor-Method执行此操作,如我在评论中所述。

相关问题