Asp.net Mvc 3 - Dropdownlistfor Fails When Url Parameter Also Exists With Same Name As View Model Parameter
Solution 1:
What about the URL mentioned,
http://mydomain.com/path/page?myparam=myvalue
The url has a queryString with key "myparam", since the name is same as property name and When the url is invoked it will add a ModelState value against "myparam".
ModelState["myparam"] will be "myvalue".
So when the page is loaded the dropdown will select "myvalue" from drop down.
Solution 2:
This is a well known issue. It's not a bug, because this is by design. It's more of a design limitation.
There's nothing that can be done about it, just rename your parameter or rename the property.
Solution 3:
This isn't much of an answer other than to say i've experienced the same behavior in a slightly different situation.
In my case I was calling a child action from the parent action and passing in a viewmodel specific to the child action, which shared some of the same property names with the parent's ViewData. When my child action partial view was rendered, it was showing viewdata from the parent controller as opposed to what was in the viewmodel I was passing to the child action. In this situation looked like view data from the parent action was polluting my child action viewmodel when the properties both had identical names.
So it looks like the binding sometimes gets confused as to what value to bind when there are name collisions. In my case I just renamed the properties in the child viewmodel to prevent this collision, but it had me pulling my hair out figuring out why the wrong value was there.
If somebody has a real answer as to why this happens I would like to know as well. To me it comes across as a binding bug, but perhaps there is an intention here.
Post a Comment for "Asp.net Mvc 3 - Dropdownlistfor Fails When Url Parameter Also Exists With Same Name As View Model Parameter"