Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net MVC DropDownList Pre-Selected item ignored

I am facing a simular problem as outlined in the question "Html.DropDownList in ASP.NET MVC RC (refresh) not pre-selecting item"

I'm using ASP.net MVC 1.0 and need to associated a javascript call when the DropDownList is changed.

<%=Html.DropDownList("SelectList", 
                    (SelectList)ViewData["SelectList"], 
                     new { onchange="javascript:selected_droplist();" } )%>

This is all good except it is ignoring my pre-selected item

If I remove the extra functionality:

<%=Html.DropDownList("SelectList")%>

It is happy and will use my pre-selected item. BUT i don't get the Javascript action!

So, how do i add the javascript to the onchange event?

like image 695
Andrew Harry Avatar asked Jun 11 '09 02:06

Andrew Harry


1 Answers

Found the Answer

When the Name given to the control (the first parameter in this case being "SelectList") is the same as one of the Keys in the ViewData dictionary basically it screws up and ignores the pre-selected item in the SelectList

By simply renaming the DropDownList it works correctly and binds to the Pre-Selected item

like image 87
Andrew Harry Avatar answered Oct 19 '22 22:10

Andrew Harry