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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With