0

I'm trying to populate a DropDownList and setting the default value of it to the first element of the list, however the dropdown gets populated but as much as i try with different approaches, I cant set the default value. I have a controller method that creates the selectlist and pass it to the view in the viewbag like this:

List<society> societyList= TiposCollection.getSociety();
List<SelectListItem> items = new List<SelectListItem>();

foreach (society societyItem in societyList)
{
    SelectListItem item = new SelectListItem
    {
        Text = society.nombre,
        Value = society.id.ToString(),
    };
    items.Add(item);
}
items.FirstOrDefault().Selected = true;
ViewBag.sociedad_emisora = new SelectList(items);

And in the view I create the DropDownList in this way:

@Html.DropDownListFor(x => x.ov.sociedad_emisora,
                     (SelectList)ViewBag.sociedad_emisora,
                     "--sociedad emisora--", 
                     new { @class = "input-lm form-control"})

The dropdown gets populated but there is no selected vale. Any suggestion?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Jose
  • 27
  • 8
  • And as a side note, using `new SelectList();` to create a 2nd `SelectList` from the 1st one is pointless extra overhead - its just `ViewBag.sociedad_emisora = items;` (but you should be using a view model with a property `IEnumerable` rather than `ViewBag`). And as explained in the dupe, remove `items.FirstOrDefault().Selected = true;` –  Jan 24 '17 at 11:37
  • Just change this: `ViewBag.sociedad_emisora = new SelectList(items);` to this `ViewBag.sociedad_emisora = new SelectList(items,"id","nombre");` and also remove the `"--sociedad emisora--",` from your view. Then it works. – Salah Akbari Jan 24 '17 at 11:43
  • @S.Akbari, no it does not. `items` is `List` and `SelectListItem` does not contain properties `id` or `nombre` and that would throw an exception (but its pointless anyway as I noted above). And why would OP want to remove the `null` label option –  Jan 24 '17 at 11:51
  • @StephenMuecke I didn't notice to item's type. So it should be `ViewBag.sociedad_emisora = new SelectList(societyList,"id","nombre");` this should works. And also he doesn't need `items` anymore. – Salah Akbari Jan 24 '17 at 11:54
  • @S.Akbari, No. Its utterly pointless - read my first comment. And you need to read the to understand why it does not bind - its the value of the property (`ov.sociedad_emisora`) that determines what is selected and OP has not set its value so the first option is selected (because something has to be) –  Jan 24 '17 at 11:58
  • 1
    Just by removing the "--sociedad emisora--", part from the html helper it takes the first item of the listt as default. thanks for the help – Jose Jan 24 '17 at 11:59
  • @StephenMuecke As I said already the `"--sociedad emisora--"` should be removed from the view, if we want the first item to be selected! based on the OP that mentioned: *and setting the default value of it to the first element of the list* – Salah Akbari Jan 24 '17 at 12:00
  • @S.Akbari, Have you bother to read the dupe! And the `"--sociedad emisora--"` should NOT be removed –  Jan 24 '17 at 12:05
  • @StephenMuecke And have you read the OP's last comment? *Just by removing the "--sociedad emisora--", part from the html helper it takes the first item of the listt as default. thanks for the help*. This helped the OP to solve his problem! – Salah Akbari Jan 24 '17 at 12:08
  • @S.Akbari. And all that does is select the first item because something has to be - its still not binding to anything. And what if OP later wanted to select say 2nd or last item –  Jan 24 '17 at 12:11
  • Yep it works, probably this "--sociedad emisora--" was overriding the selected value. Anyway i apreciate the effort from all of You, there is no point to fight. – Jose Jan 24 '17 at 14:40
  • @Jose, `"--sociedad emisora--"` is not overriding anything! It just adds a `null` label option (which you should be doing for validation) as the first option. You need to read and understand the dupe instead of using hacks. Initialize an instance of your model in the GET method, and set the value of the property your binding to and return the view - e.g. `var model = new YourModel; model.ov.sociedad_emisora = someValue; return View(model);` so that you get correct model binding. –  Jan 24 '17 at 22:42
  • @StephenMuecke okok i get it now. i dont even need to create the select list and the selectlistitems, with a normal list and binding to the property works perfect as you said – Jose Jan 25 '17 at 17:17

0 Answers0