如何在模型中对SelectList进行硬编码?

时间:2019-06-11 12:56:26

标签: c# html asp.net-mvc asp.net-core

我正在尝试对模型中的列表进行硬编码,然后在View中获取它,但我没有得到我的列表,只是System.Web.Mvc.SelectListItem的列表

型号

    [Required]
    [DisplayName("BUD")]
    public IEnumerable<string> BUD { get; set; }
    public SelectList Lista = new SelectList(new[]
    {
        new SelectListItem { Text = "BU"},
        new SelectListItem  {Text = "Marketing"},
        new SelectListItem { Text = "Additional"},
    }, "Text");

视图

@Html.DropDownListFor(model => model.BUD, new SelectList(Model.Lista), new { @class = "form-control" })

1 个答案:

答案 0 :(得分:2)

  public List<SelectListItem> Lista = new List<SelectListItem>
    {
        new SelectListItem() { Text = "BU", Selected = true, Value = "1"},
        new SelectListItem() { Text = "Marketing", Selected = false, Value = "2"},
        new SelectListItem() { Text = "Addition", Selected = false, Value = "3"}
    }

也使用Model.Lista代替new SelectList(Model.Lista)

这应该有效。