这是我的模型:
public class MyModel{
public string Name { get; set; }
public string listType { get; set; }
public string SelectedItem { get; set; }
}
我有一个部分的看法:
@model List<MyModel>
@{
SelectListItem iEqualTo = new SelectListItem { Text = "Equel To", Value = "EqualTo" };
SelectListItem iNotEqualTo = new SelectListItem { Text = "Not Equal To", Value = "NotEqualTo" };
SelectListItem iGreaterThan = new SelectListItem { Text = "Greater Than", Value = "GreaterThan" };
SelectListItem iLessThan = new SelectListItem { Text = "Less Than", Value = "LessThan" };
SelectListItem iBetween = new SelectListItem { Text = "Between", Value = "Between" };
List<SelectListItem> MyFirstList = new List<SelectListItem>() {
iEqualTo, iNotEqualTo, iGreaterThan, iLessThan, iBetween
};
List<SelectListItem> MySecondList = new List<SelectListItem>() {
iEqualTo, iNotEqualTo
};
}
@foreach(var item in Model) {
if(item.listType =="Firsttype"){
<span> @Html.DropDownList(item.Name , MyFirstList) </span>
} else {
<span> @Html.DropDownList(item.Name , MySecondList) </span>
}
}
所以我需要Selected = true
在那个项目List<SelectListItem>
中DropDownList
哪个名称等于MyModel.SelectedItem
?你的建议是什么?实际上,我的视图有更多SelectListItem
,List<SelectListItem>
并且由于某些限制,我无法将其List<SelectListItem>
作为我的模型属性传递,我需要在视图中处理它。我认为通过 lambda inline 处理它,但我不知道是否可行,如下所示:@Html.DropDownList(item.Name , MySecondList.each(a=> if (a.ToString() == item.SelectedItem) {a.Selected = true})
有什么办法吗?如果我错误地使用内联 lambda 是否有其他方法来处理这个问题?