Im using asp.net mvc 2. I have a model Supermodel that consists of 2 models TestModel1 and TestModel2.
In SuperModelView Im doing the following thing:
<%: Html.DisplayFor(x=> x.TestModel1, "TestModel1Template") %>
Its working just fine, except for the fact, that dropdownlist is populated but selected value is not set.
Im using the following code for a dropdownlist in my template:
<%: Html.DropDownListFor(x=> x.Property1, (IEnumerable<SelectListItem>)ViewData["MyDDLList"], Model.Property1) %>
and its not setting the selected property. I put the code below to SuperModelView, that calls <%: Html.DisplayFor
To populate the template and it works just fine. So I`m kinda puzzled, what is the difference?
<%: Html.DropDownListFor(x=> x.TestModel1.Property1, (IEnumerable<SelectListItem>)ViewData["MyDDLList"], Model.TestModel1.Property1) %>
UPDATE: I`ve tried to investigate the issue, but something is totally wrong. I can share the whole code, not sure where to put it, here or attach with separate files.
@Darin, what other parts should I share, or just share the whole model view and controller files?
Firstly display templates are just for displaying. If you need to edit with drop down use editor template:
and in your editor template:
where MyDDLList is defined like:
and in your controller action you fill the values:
UPDATE:
Here’s a complete working example:
Model:
Controller:
View (
~/Views/Index.aspx):DisplayTemplate (
~/Views/DisplayTemplates/MyViewModel):