在 C# MVC5 Internet 应用程序 View 中,如何显示下拉列表供用户选择从 View Model
填充的列表项名单?
这是 ViewModel
代码:
public class MapLocationItemViewModel
{
[Editable(false)]
public int mapLocationForeignKeyId { get; set; }
public List<string> mapLocationItemTypes { get; set; }
public MapLocationItem mapLocationItem { get; set; }
}
这是我目前在 View
中的代码:
<div class="form-group">
@Html.LabelFor(model => model.mapLocationItem.mapLocationItemType, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.mapLocationItemTypes)
</div>
</div>
mapLocationItemTypes
中的每一项当前显示为 class="text-box single-line valid"
.
是否有一个 MVC View 标签将显示一个列表,该列表由 list<string>
填充或 array
?
我试过下面的代码:
@Html.DropDownListFor(model => model.mapLocationItemTypes)
但是,我遇到编译错误,并且不确定重载方法的值。
在 View 中显示列表的最佳方式是什么,以便用户可以从列表中选择列表项?
提前致谢
最佳答案
型号:
public List<SelectListItem> mapLocationItemTypes { get; set; }
public int mapLocationItemVal { get; set; }
查看:
@Html.DropDownListFor(m => m.mapLocationItemVal , new SelectList(Model.mapLocationItemTypes , "Value", "Text"), " -----Select List----- ")
在上面的示例中,DropDownListFor()
的第 3 个参数即 "-----Select List----- "
将是初始选择的项目您的下拉菜单的值等于 ''
。
在 POST 期间的 Controller mapLocationItemVal
将选择下拉值。
上面显示的代码是在 MVC 中绑定(bind)下拉列表的最佳和最简单的方法。
关于c# - MVC5 View 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25036799/