ASP.NET MVC DropDownList SelectedValue works on Edit action, but not Create action

Posted by davekaro on Stack Overflow See other posts from Stack Overflow or by davekaro
Published on 2010-05-19T21:26:33Z Indexed on 2010/05/19 21:30 UTC
Read the original article Hit count: 964

Filed under:
|

I have the following code in my controller (for Edit and Create):

model.Templates = new SelectList(PageManagementService.PageTemplateFetchList(), "PageId", "Title", 213);

the "213" is an Id for one of the pages - just using it for testing.

And this is in my view (for Edit and Create):

<%= this.Html.DropDownListFor(model => model.Page.TemplateId, this.Model.Templates)%>
<%= this.Model.Templates.SelectedValue %>

When I go to the Create form, I see the dropdown list, but the tag with value="213" is not selected. I even output the SelectedValue to make sure it's 213 - and I see 213.
When I go to the Edit form, I see the dropdown list, and the tag with value="213" is selected.

On the Create form, none of the tags have a "selected" attribute.
On the Edit form, the tag with value="213" has the "selected" attribute.

Am I missing something? What could be causing this? Anyone see this behavior before?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about dropdownlist