开发者

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

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 causin开发者_Go百科g this? Anyone see this behavior before?

UPDATE: Changing the name of the dropdownlist makes it work. For example, instead of

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

I did

<%= this.Html.DropDownList("somedropdown", this.Model.Templates)%>

and it worked. Not sure why though...


This could occur because the DataValueField is a string object and it might have a type mismatch there.

Try something like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜