Why isn't modelbinding working on a DropDown?
What am I doing wrong?
To illustrate my problem, I did the following:
Created a new asp.net MVC project (beta1) Created the following class:
HomeViewData : ViewDataDictionary
+List : SelectList
Added a viewdata to the HomeController class as following
Homecontroller: Controller
+HomeViewData vd = new HomeViewData
Added the folowing lines to my Home.Index() method
vd.list = new SelectList(new List<string>(){"test", "test1", "test2"});
return View("Index", vd);
And replicated the same method but with a different signature to catch postback calls
[AcceptVerps(HttpVerbs.Post)]
public ActionResult Index(HomeViewdata d)
Here I added a breakpoint to the first line of code.
Then I added the following code to my Index.aspx
<%Html.BeginForm();%>
<%=Html.DropDownList("List")%>
<%Html.EndForm();%>
As a result, I get a page that has a nice dropdown with the values test, test1 and test2.
Then after selecting a value I press enter (didn't bother with a submit button) and see how my breakpoint halts the code. I check the value of the HomeViewData...
NULL
If I do the same exercise but with a textbox for example, it all works fine...
--EDIT--
Things I tried since this post:
- Adding a [Bind(Prefix="")] attribute to my action as suggested by Phil
- Adding a [Bind(Prefix=" ")] to see if this would remove the error of "empty name not allowed"
- Adding the name of my action's parameter in my control's name
<%=Html.DropDownList("d.List")%>