3

I have a dropdown in View

@Html.DropDownList("GenderAllowed", (SelectList)ViewBag.LocGenederAllowedList, new { @class = "form-control" })

and I am sending list of dropdown through ViewBag, and through model I am sending value that need to be selected in the dropdown.

But the value in dropdown is not selected.

My Controller

[HttpGet]
    public ActionResult EditVendorLocation(int VendorLocationID)
    {
        VendorLocationHandler obj = new VendorLocationHandler();
        LocationGrid objLoc = new LocationGrid();
        FillGenederAllowed(objLoc);
        objLoc = obj.GetVendorLocationForAdmin(VendorLocationID);

        return View(objLoc);
    }

Function for Viewbag

public void FillGenederAllowed(LocationGrid V)
{
    Dictionary<int, string> LocGenederAllowed = EnumHelper.GetGenderStates();
    SelectList LocGenederAllowedList = new SelectList(LocGenederAllowed, "key", "value");
    ViewBag.LocGenederAllowedList = LocGenederAllowedList;
}
justcurious
  • 839
  • 3
  • 12
  • 29
user4102564
  • 33
  • 1
  • 10
  • Model is a list of values. Did you set any of them as `Selected = true`? – teo van kot Nov 24 '15 at 08:49
  • Please refer below links http://stackoverflow.com/questions/15881575/get-the-selected-value-of-a-dropdownlist-asp-net-mvc http://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc4 – Nisha Salim Nov 24 '15 at 08:50
  • Possible duplicate of [dropdownlist set selected value in MVC3 Razor](http://stackoverflow.com/questions/6807256/dropdownlist-set-selected-value-in-mvc3-razor) – Nikhil Vartak Nov 24 '15 at 08:50
  • Just set the value of property `GenderAllowed` to match the value of one of your options and it will be selected. –  Nov 24 '15 at 10:06

4 Answers4

1

The SelectListItemsyou are passing to the DropDownList have a property Selected. In your ViewModel, set this to true for the item that should be selected initially.

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
0

You can do it in your controller action like following. Hope it helps.

ViewBag.LocGenederAllowedList = new SelectList(items, "Id", "Name", selectedValue);

dot net fiddle link: https://dotnetfiddle.net/PFlqei

Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
0

Take a look at this class. All you need to do is create instances of them and set the Selected property to true for the item you want to be initially selected:

public ActionResult YourActionMethod(...)
{
    var selectItems = Repository.SomeDomainModelObjectCollection
      .Select(x => new SelectListItem {
        Text = x.SomeProperty,
        Value = x.SomeOtherProperty,
        Selected = ShoudBeSelected(x)
    });
    ViewBag.SelectListItems = selectItems;
    // more code
    var model = ...; // create your model
    return View(model);
}

You will need this overload of Html.DropDownListFor(...) in order to use this.

justcurious
  • 839
  • 3
  • 12
  • 29
Balázs
  • 2,929
  • 2
  • 19
  • 34
0

You need this in your controller

   ViewBag.LocGenederAllowedList = 
       new SelectList(db.SomeValues, "Value", "Text",selectedValue);

And in your view

            @Html.DropDownList("GenderAllowed", 
         (SelectList)ViewBag.LocGenederAllowedList, new { @class = "form-control" })
Bellash
  • 7,560
  • 6
  • 53
  • 86
  • This is not workingSelectList LocGenederAllowedList = new SelectList(LocGenederAllowed, "key", "value", GenderAllow); – user4102564 Nov 24 '15 at 10:43