MVC DropDownList selected value not working
Asked Answered
S

2

9

I am using MVC 5.

I have my ViewBag for list as

ViewBag.TitleList = new SelectList((new string[] { "Mr", "Miss", "Ms", "Mrs" }), contact.Title);
//contact.Title has selected value.

then I tried converting the array to SelectListItem ( to no avail)

On the View it looks like

@Html.DropDownListFor(model => model.Title, ViewBag.TitleList as SelectList, "Select")

also I tried

@Html.DropDownList("Title", ViewBag.TitleList as SelectList, "Select")

The list loads successfully but the Selected Value is Not selected. How to Fix this problem?

Update The culprit was ViewBag.Title matching my model.Title. Renamed my model property to something else and it worked. Arrgh!

Someway answered 13/11, 2014 at 9:45 Comment(4)
Using @Html.DropDownListFor() if the value of model.Title is (say) "Miss", then the 2nd option will be selected (you do not need the 2nd parameter in the SelectList constructor). Check that the value of Title matches exactly one of the option values.Mesopotamia
Yes the value in Title matches with the ones in the listSomeway
May be this is a duplicate to that post. [Possible duplicate][1] [1]: #5859179Castara
Thank you for your update. I have spent 30 minutes wondering why my Title dropdown wasn't being Selected. I never knew this was an issue before today - thank you!Graphophone
P
10

Set value of the Title property in the controller:

ViewBag.TitleList = new SelectList(new string[] { "Mr", "Miss", "Ms", "Mrs" });
viewModel.Title = "Miss"; // Miss will be selected by default

Another possible reason (and the correct one, based on the comments below) is that ViewData["Title"] is overridden by another value. Change name of the Title property to any other and everything should work.

Penza answered 13/11, 2014 at 9:54 Comment(5)
The Title already has value while loading. it is part of edit method.Someway
@Ruchan, then the value of Title does not match one of those 4 values (perhaps check for white space), or your trying to do this in a loopMesopotamia
@Ruchan, Is it any chance you're setting the title of the page using Title as a key: ViewBag.Title = "Edit"? Try to change the property name to any other.Penza
I thought the same and changed the model property name and tried, but still the sameSomeway
Ok, at the end it was eventually the ViewBag.Title..... so mad right now. Edit your answer so i can choose it as correct one.Someway
W
2

When a value is not specified (i.e. "Id"), DropDownListFor sometimes does not behave properly. Try this:

public class FooModel
{
    public int Id { get; set; }
    public string Text { get; set; }
}

var temp = new FooModel[] 
{
    new FooModel {Id = 1, Text = "Mr"}, new FooModel {Id = 2, Text = "Miss"},
    new FooModel {Id = 3, Text = "Ms"}, new FooModel {Id = 4, Text = "Mrs"}
                       };
ViewBag.TitleList = new SelectList(temp, "Id", "Text", 2);

EDIT: other sololution

var temp = new []
{
    new SelectListItem {Value = "Mr", Text = "Mr"}, new SelectListItem {Value = "Miss", Text = "Miss"},
    new SelectListItem {Value = "Ms", Text = "Ms"}, new SelectListItem {Value = "Mrs", Text = "Mrs"}
};
ViewBag.TitleList = new SelectList(temp, "Value", "Text", "Miss");
Whelan answered 13/11, 2014 at 11:36 Comment(1)
See Ivan, when you solve a problem all by yourself, it's called sololution.Fletcher

© 2022 - 2024 — McMap. All rights reserved.