@ Html.DropDownList () Does not show the selected value of the ViewData ["something"]

0

I have a SelectListItem and in an element I put Selected = true, but when passing this object to razor in an @ Html.DropDownList () it does not recognize the selected element.

This is how I have it in my view Razor

@Html.DropDownList("ListaProgramas", (IEnumerable<SelectListItem>)ViewData["ListaProgramas"], new {@class = "form-control pull-left", @id = "ListaProgramas", @style = "color:black; width:100%;" })

List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
                        {
                            Text = row["ProgramName"].ToString().Trim(),
                            Value = row["Id"].ToString().Trim(),
                            Selected = valor

                        });

I put a single element of this list as selected, that is the one I want to appear as selected in razor view.

    
asked by Elvin Acevedo 29.05.2017 в 19:36
source

2 answers

1

Let's see if I explain myself better in this way. In the Employees variable I have stored a list of employees, which have: "Id", "Name" and "Surnames". That list I want to render it in a view using the helper @HtmlDropDownList but I want it to be selected by default x employee. This is what Elvin Acevedo needs.

on my controller:

 ViewBag.EmpleadoId = new SelectList(Empleados, "Id", "Nombre", 1);

I use ViewBag but that is not relevant because it is the same as ViewData. That question would be good. "Differences between ViewBag and ViewData". Returning to the topic, the first value passed as parameter in my ViewBag is Employees, which is the list of employees I want to show, the second value represents the value that I will pick up when selecting, in this case I want to collect as "select value" the id of the selected employee, the third parameter is the field that I will show in my select list, as you can see, I have "Name", so in my select list when I display it I will see the names of all the employees, and now it is where the fourth parameter that is optional but represents the "select value" that my friend Elvis wants by default, as I am collecting the "Id" since I have defined it in the second parameter so in the fourth parameter I should put the value of an Id, as I defined it above in the example of my ViewBag. Now let's call this in the view

@Html.DropDownList("EmpleadoId",null, String.Empty, new {aki lo de siempre para html options como class y id})

this will show me a select list with the names of all the employees and selected by default the employee whose Id is 1. Greetings

    
answered by 30.05.2017 / 21:05
source
1

Your DropDownList should be called different than the object you are placing in the ViewData (or ViewBag).

@Html.DropDownList("ListaProgramasHtmlSelect", (IEnumerable<SelectListItem>)ViewData["ListaProgramas"], new { @class = "form-control pull-left", @id = "ListaProgramas", @style = "color:black; width:100%;" })

or

@Html.DropDownList("ListaProgramas", (IEnumerable<SelectListItem>)ViewData["ListaProgramasViewData"], new { @class = "form-control pull-left", @id = "ListaProgramas", @style = "color:black; width:100%;" })
    
answered by 30.05.2017 в 19:48