Create static Selectlist and pass a selected item to the View

Asked

Viewed 9,136 times

7

I’m having a question about how to send my selectList "status" to my View EditarUsuario.

I 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 it to come set the current user status. 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’ve seen, this property Status is a normal field that brings 1 to Active and 0 to Inactive. In my View put the Helper in this way:

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

And when checking the generated HTML this way:

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

How do I set the list like this?

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

2 answers

7


Following your model, I will change only the way to pass the data to the view. In place of a Selectlist I will use the Selectlistitem, and in place of ViewData, one 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.

And in his view, just call the DropDown, being like this:

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

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

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

  • Perfect, it worked exactly as I would like. Thank you very much @Randrade...

  • thank you very much!

5

@Html.DropDownList becomes quite limited when you send a SelectList ready. Particularly, I prefer that the View pile that DropDownList to me as follows:

@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 each SelectListItem, I can assemble the list with a Status already selected.

Obviously, Status has to be a class to work:

ViewBag.StatusPossiveis = new List<Status> {
    new Status {StatusId = "1", Name="Ativo" },
    new Status {StatusId = "0", Name="Inativo" }
};
  • 1

    Your tips are extremely valuable. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.