Georgi Georgiev
Georgi Georgiev

Reputation: 3964

ASP.NET MVC @Html.DropdownListFor wrong selected value

I know there are tons of question about this (this this and this) are some of the questions I read, but I did not found anything that helped:

In my View I have

@Html.DropDownListFor(m => m.AktPriority, CustomHTMLHeper.GetPriorityDropDown(),new { @class = "form-control" })

In my helper

    public static SelectList GetPriorityDropDown()
    {

        List<SelectListItem> result = new List<SelectListItem>();
        SelectListItem low = new SelectListItem();
        low.Value = "0";
        low.Text = "Niedrig";
        SelectListItem middle = new SelectListItem();
        middle.Value = "1";
        middle.Text = "Mittel";
        SelectListItem high = new SelectListItem();
        high.Value = "2";
        high.Text = "Hoch";
        return new SelectList(new SelectListItem[] { middle, high, low }, "Value", "Text", "1");

    }

I expect the item with the value 1 to be selected but following HTML is generated:

<select name="AktPriority" id="AktPriority" class="form-control">
  <option value="1">Mittel</option>
   <option value="2">Hoch</option>
    <option value="0" selected="selected">Niedrig</option>
   </select>

I also tried setting the Selected Property to true in the helper. This did not work either.

Upvotes: 0

Views: 2577

Answers (6)

Fred
Fred

Reputation: 12846

It seems the @Html.DropDownListFor acts weird (sets the wrong option as selected) when the property is null.

@Html.DropDownListFor(model => model.Car, Model.Cars, "Pick a car…")

I solved it by setting the Car property in the model to string.Empty when it is null.

Upvotes: 0

mhapps
mhapps

Reputation: 1133

Here is another possibility (after expiring model and SelectedValue solutions).

ViewBag.AktPriority = 0;

@Html.DropDownListFor(m => m.AktPriority, CustomHTMLHeper.GetPriorityDropDown(),new { @class = "form-control" })

For some unknown reason I've found MVC will prefer to use the ViewBag value instead of the models if the variable name is the same. Solution was to rename one.

Upvotes: 0

Gjohn
Gjohn

Reputation: 1281

In your helper add the following line

middle.Selected = true;

I believe by default if you do not select anything the control selects the very first value in the list.

Sorry my bad, did not see that line. Try populating result and then in your return of SelectList do

return new SelectList((IEnumerable<SelectListItem>)result, "Value", "Text", "1");

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239460

Since you have tried both setting the SelectListItem's Selected property to true and actually specifying the selected value when creating the SelectList, there's only one scenario where I can still see this failing. You didn't say definitively, but my assumption is that this is only a problem on initial page load of something like a create view, where you have no posted data and no existing data.

In that one scenario, your ApkPriority property, which I'm assuming is an int because you haven't specified that either, would have a default value of 0. Razor, is then, auto-selecting the item that matches this default value. If you want the option with value 1 selected by default, you should actually make it the default value of the property:

private int? apkPriority;
public int ApkPriority
{
    get { return apkPriority ?? 1; }
    set { apkPriority = value; }
}

Upvotes: 2

Georgi Georgiev
Georgi Georgiev

Reputation: 3964

While copying some more code to provide more information in the OP I found the mistake myself.

In my Controller get-Method, which returns the empty form I had:

[HTTPGet]
 ActionResult Create () 
  {
     return View(new ActivityModel())
  }

In my Model the value of the selected value is saved as an int. So when I create a new empty model as shown above the default value of int is overriding the default value of 1.

The solution was simple

[HTTPGet]
 ActionResult Create () 
  {
     ActivityModel = new ActivityModel();
     model.AktPriority = 1;
     return View(model)
  }

Upvotes: 1

mxmissile
mxmissile

Reputation: 11681

You did not post your action method, but if you use DropDownListFor you need to set the "selected" value in the model:

model.AktPriority = "1";

return View(model);

Upvotes: 1

Related Questions