views:

331

answers:

1

In my controller I have this:

ViewData["maskList"] = new SelectList(equipmentRepository.GetMasks(), "Id", "DisplayName");

and then I bind it to my view using

<div each="var nfa in mfa.NasalFittingAssessment">
    ${Html.DropDownList("NasalMaskTypeId", ViewData["maskList"] as IEnumerable<System.Web.Mvc.SelectListItem>, new { class = "ddl" })}                
</div>

Note that I'm using the spark view engine so this DropDownList is getting rendered to the page via a loop. This means that the selected value of the dropdown list will change on each iteration of the loop.

What I can't work out is how to pass in the value I want to set the DropDownList to based on the value that is currently being rendered to the screen.

+1  A: 

So after a bit of playing around I worked it out:

What you want to do is just assign the generic collection in the controller like this:

ViewData["maskList"] = equipmentRepository.GetMasks();

and then construct the selectlist on the fly from within the view like this:

<div each="var nfa in mfa.NasalFittingAssessment">
    ${Html.DropDownList("NasalFittingAssessment.NasalMaskType.Id", new SelectList(ViewData["maskList"] as IList<Equipment>, "Id", "DisplayName", nfa.NasalMaskType.Id), new { class = "ddl" })}                
</div>
lomaxx