来自简单列表模型的 DropDownListFor
DropDownListFor from Simple List Model
如何实现没有 Id 的简单 DropDownList。
public AddItemModel()
{
Types = new SelectList(new []{"Type1", "Type2", "Type3"});
}
public SelectList Types { get; set; }
@Html.DropDownListFor(x => x.AddItem, ???
您的视图模型需要一个 属性 来保存所选值,然后您可以试试这个:
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)
如何实现没有 Id 的简单 DropDownList。
public AddItemModel()
{
Types = new SelectList(new []{"Type1", "Type2", "Type3"});
}
public SelectList Types { get; set; }
@Html.DropDownListFor(x => x.AddItem, ???
您的视图模型需要一个 属性 来保存所选值,然后您可以试试这个:
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)