Friday, June 10, 2011

Asp.net MVC, Html.DropDownList Selected Value for EDIT

In Controller:

  ViewData["MyDropDown"] = new SelectList(CatModel.getAllCategory(), "Value", "Text",selectedValue:prModel.DisplayProductByid(prd).CategoryID);

In View:

 <%: Html.DropDownList("MyDropDownID", ViewData["MyDropDown"] as SelectList)%>

----------------------------------------------------------------------------
I have got same problem and the solution is above:
I just changed the Name of the drop down and handled the assignment in the controller.
The reason behind this problem is that asp.net MVC first looks for a match between the name of the drop down and a property on the model. If there’s a match, the selected value of the SelectList is overridden. Changing the name of the drop down is all it takes to remedy the issue.

No comments:

Post a Comment