I’m displaying a list of users. Each user record has a TrainingEvent object property (from a choice of many TrainingEvents). I’ve created a viewmodel to include the User list and the TrainingEvents list.
ViewModel:
public class RegistrationViewModel
{
public IEnumerable<Domain.Entities.User> Users { get; set; }
public IEnumerable<SelectListItem> TrainingEvents { get; set; }
}
(note: the Domain.Entities.User contains a TrainingEvent object property)
Controller:
public ActionResult Dealership(int id)
{
var model = new RegistrationViewModel
{
Users = repository.Find
.Where(u => u.Dealer.DealerId == id).OrderBy(u => u.LastName),
TrainingEvents = repository.TrainingEvents.ToList()
.Select(x => new SelectListItem
{
Text = x.Date.ToString() + " - " + x.LocationName,
Value = x.TrainingEventId.ToString()
})
};
return View("East", model);
}
Binding in View:
@foreach(Company.Domain.Entities.User usr in Model.Users)
{
<tr>
<td>@usr.LastName</td>
<td>@usr.FirstName</td>
<td>@usr.JobDescription</td>
<td>@Html.DropDownListFor(m => m.Users.FirstOrDefault(u => u.UserId == usr.UserId).TrainingEvent, Model.TrainingEvents)</td>
</tr>
}
The users are listed and each user row has a dropdown which populates with TrainingEvents. However, the value previously saved in the user’s TrainingEvent object isn’t selected. Any ideas as why not?
You are incorrectly using the
DropDownListForhelper. This helper expects to be passed as first argument a simple lambda expression containing at most a member access. In your example you are attempting to construct some complex lambda expression using things likeFirstOrDefaultextension methods which is not supported. Also it is not the responsibility of the view to be fetching some data from the model. It’s the responsibility of the controller to populate a suitable view model for the view and pass it for consumptionSo I would recommend you to use a real view model reflecting the requirements of your view:
which will be populated in your controller action:
and finally in your view bind the
DropDownListForhelper to the corresponding value in the view model: