0

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.

Community
  • 1
  • 1
Georgi Georgiev
  • 3,854
  • 5
  • 29
  • 39

5 Answers5

2

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; }
}
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
1

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);
mxmissile
  • 11,464
  • 3
  • 53
  • 79
1

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)
  }
Georgi Georgiev
  • 3,854
  • 5
  • 29
  • 39
0

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");
Gjohn
  • 1,261
  • 1
  • 8
  • 12
0

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.

mhapps
  • 1,023
  • 8
  • 15