ASP.NET MVC Populating a DropDownList with a value from the view

Posted by lomaxx on Stack Overflow See other posts from Stack Overflow or by lomaxx
Published on 2010-05-02T07:19:17Z Indexed on 2010/05/02 7:27 UTC
Read the original article Hit count: 334

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about dropdownlist