来自简单列表模型的DropDownListFor

时间:2015-01-26 17:38:35

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

如何在没有Id的情况下实现简单的DropDownList。

public AddItemModel() 
{
    Types = new SelectList(new []{"Type1", "Type2", "Type3"});
}

public SelectList Types { get; set; }

@Html.DropDownListFor(x => x.AddItem, ???

1 个答案:

答案 0 :(得分:2)

您需要在视图模型上使用属性来保存所选值,然后您可以尝试:

public AddItemModel() 
{
    var data = new [] { "Type1", "Type2", "Type3" };
    Types = data.Select(x => new SelectListItem
    {
        Value = x,
        Text = x,
    });
}

public IEnumerable<SelectListItem> Types { get; set; }
public string SelectedType { get; set; }

然后在你看来:

@Html.DropDownListFor(x => x.SelectedType, Model.Types)
相关问题