Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple roles in Asp.net MVC3

I have an admin site for the admin to create users. Here he has to chose the roles for the user - like the Asp.NET configuration site. I made 3 checkboxes 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 check them and submit, I get to the breakpoint in my Register action, but the RolesContainer is null at this point - can anyone tell me why this is?

like image 904
Mrlondon7100 Avatar asked Nov 23 '25 14:11

Mrlondon7100


1 Answers

I copied and pasted your code (except the 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 there is something wrong in your Register.cshtml file, of which we don't have to validate.

like image 84
Erik Philips Avatar answered Nov 25 '25 09:11

Erik Philips



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!