未为 ASP.Net MVC 3 DropDownListFor 设置所选值
我有一个绑定到一个视图的父视图模型,该视图包含一个属性,该属性是其他视图模型的列表。
这是父视图模型代码:
var timeAvailability = (from u in _entities.UserTimeAvailabilities
where u.UserId == userId
select u).ToList();
for(int index = 0; index < timeAvailability.Count; index++)
{
var availableTime = timeAvailability[index];
TimeAvailability.Add(new AvailableTimeSlotViewModel(index)
{
SelectedDay = availableTime.DayId,
SelectedStartTime = availableTime.StartHourId.HasValue ? availableTime.StartHourId.Value : 0,
SelectedEndTime = availableTime.EndHourId.HasValue ? availableTime.EndHourId.Value : 0
});
}
TimeAvailability 是一个列表
这是AvailableTimeSlotViewModel:
public List<SelectListItem> Days { get; set; }
public int Index { get; set; }
public List<SelectListItem> Hours { get; set; }
[Display(Name = "Select Day")]
public int SelectedDay { get; set; }
[Display(Name="Start Time")]
public int SelectedStartTime { get; set; }
[Display(Name = "End Time")]
public int SelectedEndTime { get; set; }
public AvailableTimeSlotViewModel(int index)
{
Index = index;
_entities = Repository.GetRepository();
Days = new List<SelectListItem>();
_entities.Days.ToList().ForEach(d => Days.Add(new SelectListItem { Value = d.DayId.ToString(), Text = d.Name }));
Hours = new List<SelectListItem>();
_entities.Hours.ToList().ForEach(h => Hours.Add(new SelectListItem { Value = h.HourId.ToString(), Text = h.Name }));
}
视图中绑定到 UserPreferences 的代码:
<div id="time-availability-div">
@for (int index = 0; index < Model.TimeAvailability.Count; index++)
{
<table>
<thead>
<tr>
<td>
@Html.LabelFor(m => m.TimeAvailability[index].SelectedDay)
</td>
<td>
@Html.LabelFor(m => m.TimeAvailability[index].SelectedStartTime)
</td>
<td>
@Html.LabelFor(m => m.TimeAvailability[index].SelectedEndTime)
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
@Html.DropDownListFor(m => m.TimeAvailability[index].SelectedDay, Model.Days)
</td>
<td>
@Html.DropDownListFor(m => m.TimeAvailability[index].SelectedStartTime, Model.Hours)
</td>
<td>
@Html.DropDownListFor(m => m.TimeAvailability[index].SelectedEndTime, Model.Hours)
</td>
</tr>
</tbody>
</table>
}
</div>
需要注意的一件事是,如果我在 DropDownListFor 中显式设置所选值,它就可以正常工作。我想知道为什么所选值不会自动绑定引用索引处的属性。
I have a parent view model bound to a view that contains an property which is a list of other view models.
Here is the parent view model code:
var timeAvailability = (from u in _entities.UserTimeAvailabilities
where u.UserId == userId
select u).ToList();
for(int index = 0; index < timeAvailability.Count; index++)
{
var availableTime = timeAvailability[index];
TimeAvailability.Add(new AvailableTimeSlotViewModel(index)
{
SelectedDay = availableTime.DayId,
SelectedStartTime = availableTime.StartHourId.HasValue ? availableTime.StartHourId.Value : 0,
SelectedEndTime = availableTime.EndHourId.HasValue ? availableTime.EndHourId.Value : 0
});
}
TimeAvailability is a List
Here is AvailableTimeSlotViewModel:
public List<SelectListItem> Days { get; set; }
public int Index { get; set; }
public List<SelectListItem> Hours { get; set; }
[Display(Name = "Select Day")]
public int SelectedDay { get; set; }
[Display(Name="Start Time")]
public int SelectedStartTime { get; set; }
[Display(Name = "End Time")]
public int SelectedEndTime { get; set; }
public AvailableTimeSlotViewModel(int index)
{
Index = index;
_entities = Repository.GetRepository();
Days = new List<SelectListItem>();
_entities.Days.ToList().ForEach(d => Days.Add(new SelectListItem { Value = d.DayId.ToString(), Text = d.Name }));
Hours = new List<SelectListItem>();
_entities.Hours.ToList().ForEach(h => Hours.Add(new SelectListItem { Value = h.HourId.ToString(), Text = h.Name }));
}
And the code in the view which is bound to UserPreferences:
<div id="time-availability-div">
@for (int index = 0; index < Model.TimeAvailability.Count; index++)
{
<table>
<thead>
<tr>
<td>
@Html.LabelFor(m => m.TimeAvailability[index].SelectedDay)
</td>
<td>
@Html.LabelFor(m => m.TimeAvailability[index].SelectedStartTime)
</td>
<td>
@Html.LabelFor(m => m.TimeAvailability[index].SelectedEndTime)
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
@Html.DropDownListFor(m => m.TimeAvailability[index].SelectedDay, Model.Days)
</td>
<td>
@Html.DropDownListFor(m => m.TimeAvailability[index].SelectedStartTime, Model.Hours)
</td>
<td>
@Html.DropDownListFor(m => m.TimeAvailability[index].SelectedEndTime, Model.Hours)
</td>
</tr>
</tbody>
</table>
}
</div>
One thing to note is that if I explicitly set the selected value in the DropDownListFor it works fine. I'm wondering why the selected value isn't bound automatically referencing the property at index.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您使用了复杂的集合 lambda 表达式作为第一个参数:
m => m.TimeAvailability[index].SelectedEndTime
。不幸的是,DropDownListFor
帮助器不支持自动设置此类复杂表达式的默认值,您需要设置选定的值:Because you used a complex collection lambda expression as first argument:
m => m.TimeAvailability[index].SelectedEndTime
. Unfortunately theDropDownListFor
helper doesn't support automatically setting the default value for such complex expressions and you need to set the selected value: