In the second part of this MVC tutorial we add a dropdown list to select the user role when creating or editing a user.
In the User model we must add a new field that represents an interface that stores the options:
public IEnumerable<System.Web.Mvc.SelectListItem> perfis { get; set; }
Now in the User controller the Create and Edit functions must be changed. Each function must add the options to the perfis interface before showing the view. Something like this:
// GET: Users/Create
public ActionResult Create()
{
//perfis options for the dropdownlist
var user = new User();
user.perfis = new[] {
new SelectListItem{Value="0",Text="Admin"},
new SelectListItem{Value="1",Text="User"},
};
return View(user);
}
In the views the text box must be deleted and a drop down must be added to the field.
The line that generates the drop down is this:
@Html.DropDownListFor( model=>model.perfil,new SelectList(Model.perfis,"Value","Text"))
Youtube video
Project code in Github
In the User model we must add a new field that represents an interface that stores the options:
public IEnumerable<System.Web.Mvc.SelectListItem> perfis { get; set; }
Now in the User controller the Create and Edit functions must be changed. Each function must add the options to the perfis interface before showing the view. Something like this:
// GET: Users/Create
public ActionResult Create()
{
//perfis options for the dropdownlist
var user = new User();
user.perfis = new[] {
new SelectListItem{Value="0",Text="Admin"},
new SelectListItem{Value="1",Text="User"},
};
return View(user);
}
In the views the text box must be deleted and a drop down must be added to the field.
The line that generates the drop down is this:
@Html.DropDownListFor( model=>model.perfil,new SelectList(Model.perfis,"Value","Text"))
Youtube video
Comentários
Enviar um comentário