Multiple roles in Asp.net MVC3

I have an admin site for admin to create users. Here he should choose roles for the user - for example, the Asp.NET configuration site. I made 3 flags with different roles.

[Authorize(Roles = "Admin")]
    [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {

            var Rolemodel = model.RolesContainer; 
            // Attempt to register the user
            MembershipCreateStatus createStatus;
            Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
            if (createStatus == MembershipCreateStatus.Success)
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", ErrorCodeToString(createStatus));
            }
        }


        // If we got this far, something failed, redisplay form
        return View(model);
    }


   [Authorize(Roles = "Admin")]
    public ActionResult Register()
    {
        List<SelectListItem> tempRoles = new List<SelectListItem>();
        tempRoles.Add(new SelectListItem{ Text = "Admin", Selected = false, Value = "Admin" });
            tempRoles.Add(new SelectListItem{ Text = "Production", Selected = false, Value = "Production"});
                tempRoles.Add(new SelectListItem{ Text = "Sale", Selected = false, Value = "Sale"});

                return View(new RegisterModel { RolesContainer = tempRoles });
    }

---- View --------

@{ foreach (var item in Model.RolesContainer)
               {
                    @Html.DisplayFor(m => item.Text)
                    @Html.CheckBoxFor(m => item.Selected)
               }
            }

When I test them and submit, I hit the breakpoint in my action Register, but RolesContaineris null at this point - can anyone tell me why this is?

+3
source share
3 answers

I copied and pasted your code (except cshtml) into the following files, and it works as expected.

HomeController.cs

using System.Collections.Generic;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            List<SelectListItem> tempRoles = new List<SelectListItem>();
            tempRoles.Add(new SelectListItem { Text = "Admin", 
                                               Selected = false, 
                                               Value = "Admin" });
            tempRoles.Add(new SelectListItem { Text = "Production", 
                                               Selected = false, 
                                               Value = "Production" });
            tempRoles.Add(new SelectListItem { Text = "Sale", 
                                               Selected = false, 
                                               Value = "Sale" });

            return View(new RegisterModel { RolesContainer = tempRoles });
        }

    }
}

RegisterModel

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace MvcApplication1.Models
{
    public class RegisterModel
    {
        [Required]
        [Display(Name = "Brugernavn")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email adresse")]
        public string Email { get; set; }

        public List<SelectListItem> RolesContainer { get; set;}
    }
}

Home /Index.cshtml

@model MvcApplication1.Models.RegisterModel

@{ 
    foreach (var item in Model.RolesContainer)
    {
        @Html.DisplayFor(m => item.Text)
        @Html.CheckBoxFor(m => item.Selected)
    }
}

Most likely, something is wrong in your Register.cshtml file, which we do not need to check about.

+2
source

, RolesContainer, . :

public class RegisterModel
{
    public RegisterModel() {
        RolesContainer = new List<SelectListItem>;
    }

    // rest of your code
}

, List<> , .

0

If I understand well (maybe I'm completely wrong), the model you send to the httppost register (RegisterModel) does not contain the RolesContainer you expect. I think asp.net mvc cannot match multiple checkboxes with List<SelectListItem>, ListBox should be better

0
source

All Articles