I've scoured the internet, and rewritten my code a few different ways now, and I am unable to have the drop down list mark as selected the current value for the model.
This is how I've done it before, and for some reason its not working in MVC 4 beta. Is there a bug I'm not aware of or something?
still not fixed in MVC4? haha, this is so ridiculous!
you can google hundreds of discussions regarding this topic, but nobody has ever offered a reasonable solution.
it seems to me that the DropDownList and DropDownListFor helpers are broken since their very first release.
this may help you track down the problem:
We look at model state to determine what the default (selected) value should be.</div>
If there is nothing in model state, we use ViewData[name] for the selected value
If we don't find an entry in ViewData, we fall back to using the item in the selectList that has its Selected property set to true
The part that isn't working is that the dropdown list isn't selecting the model's value for that item. The value in model.Status is not the selected value when the page is loaded, even though its in the list of items to select. I just get a select box with
options, none of which have the "selected" attribute...
i understood your problem
inspect ViewData.ModelState and see if it contains a key with the name of your conrol ("status")
if so, the selectedValue is taken from there
try ModelState.Clear() to fix it
siromega
Member
3 Points
10 Posts
MVC 4 DropDownListFor
May 02, 2012 09:05 PM|LINK
I've scoured the internet, and rewritten my code a few different ways now, and I am unable to have the drop down list mark as selected the current value for the model.
This is how I've done it before, and for some reason its not working in MVC 4 beta. Is there a bug I'm not aware of or something?
<div class="editor-field"> @Html.DropDownListFor(model => model.Status, new SelectList((List<SCCM.Models.LookUpItem>) ViewData["Status"], "Value", "Name")) @Html.ValidationMessageFor(model => model.Status) </div>public class LookUpItem { public string Value { get; set; } public string Name { get; set; } public string Desc { get; set; } }Ravi theja
Member
30 Points
20 Posts
Re: MVC 4 DropDownListFor
May 03, 2012 12:38 PM|LINK
Can you give some more details regerding your problem
Huske
Contributor
4060 Points
756 Posts
Re: MVC 4 DropDownListFor
May 03, 2012 12:57 PM|LINK
Which part exactly is not working?
Doctor Sid
Member
156 Points
46 Posts
Re: MVC 4 DropDownListFor
May 03, 2012 01:12 PM|LINK
still not fixed in MVC4? haha, this is so ridiculous!
you can google hundreds of discussions regarding this topic, but nobody has ever offered a reasonable solution.
it seems to me that the DropDownList and DropDownListFor helpers are broken since their very first release.
this may help you track down the problem:
siromega
Member
3 Points
10 Posts
Re: MVC 4 DropDownListFor
May 03, 2012 03:46 PM|LINK
The part that isn't working is that the dropdown list isn't selecting the model's value for that item. The value in model.Status is not the selected value when the page is loaded, even though its in the list of items to select. I just get a select box with options, none of which have the "selected" attribute...
Young Yang -...
All-Star
21345 Points
1818 Posts
Microsoft
Re: MVC 4 DropDownListFor
May 04, 2012 09:48 AM|LINK
Hi
It is because you havn't set the default value of it. Use it like this:
@Html.DropDownListFor(model => model.Status, new SelectList((List<SCCM.Models.LookUpItem>) ViewData["Status"], "Value", "Name"), "---Select A Dessert ---")
Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
Doctor Sid
Member
156 Points
46 Posts
Re: MVC 4 DropDownListFor
May 04, 2012 12:02 PM|LINK
i understood your problem
inspect ViewData.ModelState and see if it contains a key with the name of your conrol ("status")
if so, the selectedValue is taken from there
try ModelState.Clear() to fix it
Doctor Sid
Member
156 Points
46 Posts
Re: MVC 4 DropDownListFor
May 04, 2012 12:04 PM|LINK
shouldn't the first parameter define the selected value?
can you explain what the default value has to do with this behaviour?
Ravi theja
Member
30 Points
20 Posts
Re: MVC 4 DropDownListFor
May 07, 2012 09:19 AM|LINK
Defalt 0 th index value will be selected value, for that u can see the selected value as true. Did u tried giving html attributes?
siromega
Member
3 Points
10 Posts
Re: MVC 4 DropDownListFor
May 07, 2012 02:19 PM|LINK
This doesn't help.
The HTML generatred looks like...