[英]Drop down list not populating from List MVC
I am confused on an issue with the DropDownListFor helper. 我对DropDownListFor helper的问题感到困惑。
I am trying to assign a list of values to the drop down as follows: 我正在尝试为下拉列表分配值列表,如下所示:
Model 模型
public Int32 TestID {get;set;}
public List<Int32> ListOfID {get;set;}
public IEnumerable<SelectListItem> TimeDD {get;set;}
Controller 控制者
public ActionResult Manage(int id){
MyModel model = new MyModel();
model.TimeDD = DropDownManager.TimeDD;
model.TestID = 12;
model.ListOfID = new List<Int32>{ 1,2,3,4,5,6,7};
return View(model);
}
In the view I have the following: 在视图中,我有以下内容:
@for(int i = 0; i< ListoFID.Count; i++){
<div>@Html.DropDownListFor(m=> m.TestID, Model.TimeDD)</div>
<div>@Html.DropDownListFor(m=> m.ListoFID[i], Model.TimeDD)</div>
}
The problem I am having is that the TestID drop down is working correctly however the ListoFID[i] is not selecting the values from the drop down. 我遇到的问题是TestID下拉列表正常工作,但是ListoFID [i]没有从下拉列表中选择值。 The TimeDD is a list of times as follows:
TimeDD是以下时间的列表:
/// <summary>
/// The Time dropdown
/// </summary>
/// <param name="userID"></param>
/// <returns></returns>
public static IEnumerable<SelectListItem> TimeDD()
{
// new ctl
TimeControl ctl = new TimeControl();
// get drop down
IEnumerable<SelectListItem> result = ctl.Select().Select(m => new SelectListItem { Text = m.Time1, Value = m.TimeID.ToString() }).ToList();
// clean up
ctl.Dispose();
return result;
}
Where Time1 is '09:30' and TimeID is 1 - 48. I cannot figure out how this is happening!! 其中Time1为'09:30',TimeID为1-48。我不知道这是怎么回事! As soon as I reference an object it fails to select the drop down at the correct point.
一旦我引用了一个对象,它就无法在正确的位置选择下拉菜单。
Edit Also I have a property in my model called OpeningTimes - this is the list of opening times saved against a company as below: 编辑我的模型中还有一个名为OpeningTimes的属性-这是针对公司保存的开放时间的列表,如下所示:
ComapnyID DayID StartTime EndTime
1 1 19 32 -- e.g. Monday 09:00 - 17:30
When I loop through the Opening times: 当我浏览开放时间时:
@for(int i=0; i< Model.OpeningTimes.Count; i++)
{
<tr>
<td>
@Html.DropDownListFor(m => m.OpeningTimes[i].StartTime, Model.TimeDD)
</td>
<td>
@Html.DropDownListFor(m => m.OpeningTimes[i].EndTime, Model.TimeDD)
</td>
</tr>
}
The drop downs are still not being selected. 下拉菜单仍未选中。 I can confirm the StartTime and EndTime do have values and are property int
我可以确认StartTime和EndTime确实具有值并且是属性int
DropDownListFor
requires a property to map to, not a value. DropDownListFor
需要映射的属性,而不是值。 So 所以
@Html.DropDownListFor(m=> m.ListoFID[i], Model.TimeDD) /* the [i] kills it */
will not work. 不管用。
instead of doing a list of int build it like the other 而不是像其他一样做int的列表
List<SelectListItem> ls = new List<SelectListItem>();
foreach(var temp in Model.ListOfId){
ls.Add(new SelectListItem() { Text = temp, Value = temp});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.