Avançar para o conteúdo principal

ASP.NET MVC with Entity, Identity and Migrations Part 2 - Dropdownlist

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

Comentários

Mensagens populares deste blogue

Let's make a car in Unity 3D

In this post we will make a simple car in Unity 3D. The Unity 3D physics engine is used in order to give the car a real behavior. This are the steps: [1] - Create a new Project

PacMan 3D

In this post we will be making a simple and classic game, the Pacman, with Unity 3D. Let's create a new a new project

Upgrading Windows 10 Home to Pro

 So I have been thinking about upgrading my Windows 10 Home Edition to the Pro version, but I always get to the point where it seems that I had to reinstall the entire SO and quit. After some investigating I have done it this way: - following this post  on the microsoft site I use one of the default keys for Windows 10 Pro and went to Settings > Update & Security > Activation > Change the product key; - next, Windows will activate the Pro functionalities and asks to restart; - now you have the Pro version but it's not activated, so you have to buy a Windows Pro Key. I went to UR cdkeys  and bought a key for less then €20; - and with the new key went to Change the product key and activated; - and it's done. Disclaimer : I have nothing to do with UR cdkeys so you can use any site to buy you cd key and your experience may vary from mine.