views:

207

answers:

1

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?

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...

+1  A: 

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");
Shaharyar
That didn't work. But, changing the name of the Dropdownlist did work. Any ideas?
davekaro
But saving doesn't work now, right?The problem probably lies in DropDownListFor(`model => model.Page.TemplateId`...)This is pretty complex for the `EditorForModel` method to understand. Adding a dummy int field to your model might make it work: `DropDownlistFor(model => model.DummyField, Model.Templates)`
Shaharyar
Well, saving worked because I manually grabbed the selected value from the FormCollection. I tried adding a dummy int field to my model - but that didn't work either. For now, I'm going to use what is working.
davekaro