[英]How can I display an array model property as a list in my view?
I have this array property in my model and would like to see it in my view as a dropdown list. 我的模型中有此数组属性,并且希望在我的视图中以下拉列表的形式查看它。 Here's the array property:
这是array属性:
public string[] weekDays = new string[5] { "monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
public string[] WeekDays
{
get { return weekDays; }
}
I've look for hours with no simple explanation or examples. 我找了几个小时,没有简单的解释或示例。 Please help.
请帮忙。
You can use DropDownList() html helper. 您可以使用DropDownList() html帮助器。
Html.DropDownList("weekDays",
Model.WeekDays.Select(s => new SelectListItem { Text = s }))
If you want to read the selected value you can use DropDownListFor() helper. 如果要读取选定的值,则可以使用DropDownListFor()帮助器。
Html.DropDownListFor(model => model.SelectedWeekDay, //a property to assign the value
Model.WeekDays.Select(s => new SelectListItem { Text = s, Value = s }))
Here's how I solved it. 这是我解决的方法。
@{
var wekdys = new Enrollment();
@Html.DropDownList("weekDays", wekdys.WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString() }))
}
this allows me to have a DropDownList outside of the foreach loop 这让我在foreach循环之外有一个DropDownList
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.