Brett Mathe
Brett Mathe

Reputation: 6819

Selected value not being set for ASP.Net MVC 3 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.

Upvotes: 3

Views: 1632

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

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)
)

Upvotes: 2

Related Questions