未为 ASP.Net MVC 3 DropDownListFor 设置所选值

发布于 2025-01-05 02:27:19 字数 3746 浏览 0 评论 0原文

我有一个绑定到一个视图的父视图模型,该视图包含一个属性,该属性是其他视图模型的列表。

这是父视图模型代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩心态 2025-01-12 02:27:19

我想知道为什么所选值没有自动绑定引用索引处的属性。

因为您使用了复杂的集合 lambda 表达式作为第一个参数: m => m.TimeAvailability[index].SelectedEndTime。不幸的是,DropDownListFor 帮助器不支持自动设置此类复杂表达式的默认值,您需要设置选定的值:

@Html.DropDownListFor(
    m => m.TimeAvailability[index].SelectedEndTime, 
    new SelectList(Model.Hours, "Value", "Text", Model.TimeAvailability[index].SelectedEndTime)
)

I'm wondering why the selected value isn't bound automatically referencing the property at index.

Because you used a complex collection lambda expression as first argument: m => m.TimeAvailability[index].SelectedEndTime. Unfortunately the DropDownListFor helper doesn't support automatically setting the default value for such complex expressions and you need to set the selected value:

@Html.DropDownListFor(
    m => m.TimeAvailability[index].SelectedEndTime, 
    new SelectList(Model.Hours, "Value", "Text", Model.TimeAvailability[index].SelectedEndTime)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文