Create Static SelectList and pass a selected item to View

4

I have a question about how to send my selectList "status" to my View EditarUsuario .

I've created my list with the following items:

var list = new SelectList(new[]
{
     new{ID="2",Name="Selecione"},
     new{ID="1",Name="Ativo"},
     new{ID="0",Name="Inativo"},
}, "ID", "Name");
     ViewData["list"] = list;

I would like the current status of the user to be set. I have in my model the status coming from the bank:

public int Id { get; set; }
public string CodigoUsuario { get; set; }
public string NomeUsuario { get; set; }
public int Status { get; set; }

As you have seen, this Status property is a normal field that brings 1 to Active and 0 to Inactive. In my View I put Helper like this:

@Html.DropDownList("list", ViewData["list"] as SelectList)

And checking the generated HTML looks like this:

<select id="list" name="list">
     <option value="2">Selecione</option>
     <option value="1">Ativo</option>
     <option value="0">Inativo</option>
</select>

How do I make the list look like this?

<select id="list" name="list">
     <option value="2">Selecione</option>
     <option value="1" selected>Ativo</option>
     <option value="0">Inativo</option>
</select>
    
asked by anonymous 02.10.2015 / 22:42

2 answers

4

Following your model, I will only change how to pass the data to view . Instead of a SelectList I will use SelectListItem , and instead of ViewData , a ViewBag .

Your controller would look like this:

var list = new[]
{
    new SelectListItem { Value = "2", Text = "Selecione" },
    new SelectListItem { Value = "1", Text = "Ativo" },
    new SelectListItem { Value = "0", Text = "Inativo" },
};

ViewBag.Lista = new SelectList(list, "Value", "Text");

Note that the values are now Value and Text and not ID and Name as before. p>

And in its view , just call DropDown , thus:

@Html.DropDownList("list", new SelectList(ViewBag.Lista, "ID", "Text", "1"))

Instead of 1 , you place the Value of the item that should be the selected attribute. This way, you can populate with a list of objects returned from the database, just changing the ViewBag .

  

I want to change showing better ways to get the same result.

    
02.10.2015 / 23:12
2

@Html.DropDownList becomes quite limited when you send SelectList ready. I'd rather have View mount this DropDownList to me like this:

@Html.DropDownListFor(model => model.Status, ((IEnumerable<Status>)ViewBag.StatusPossiveis).Select(option => new SelectListItem {
            Text = option.Name,
            Value = option.StatusId.ToString(),
            Selected = (Model != null) && (option.StatusId == Model.Status)
        }), "Selecione", new { @class = "form-control" })

Having access to every SelectListItem , I can mount the list with a Status already selected.

Obviously, Status needs to be a class to work:

ViewBag.StatusPossiveis = new List<Status> {
    new Status {StatusId = "1", Name="Ativo" },
    new Status {StatusId = "0", Name="Inativo" }
};
    
02.10.2015 / 23:10