MVC: display roles in checkbox list and then save them

I have a page where roles are displayed in the checkbox list, you can select the roles that you want the user to have, and then click the button to save it.

Here is my model:

public class RegisterModel
{
    [DisplayName("Roles")]
    public string[] Roles
    {
        get
        {
            return System.Web.Security.Roles.GetAllRoles();
        }
        set { }
    }
}

My opinion:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebUI.Models.RegisterModel>" %>

<% using (Html.BeginForm()) { %>
    <% foreach(string role in Model.Roles) { %>
        <input type="checkbox" value="<%: role %>" /> <%: role %>
    <% } %>

    <p>
        <input type="submit" value="Register" />
    </p>
<% } %>

And the functions from my controller:

public ActionResult Register()
{
    return View();
}

[HttpPost]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        //save roles
        return RedirectToAction("Index", "Home");
    }

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

When I try to view my page, I get the "Link to an object not set to an object instance" error in the foreach statement, which means Model.Roles is empty.

  • Am I passing Roles correctly through my model? Or should I pass roles as ViewData through my controller action?
  • If I pass roles as ViewData instead of my model, how can I access the selected elements when I submit the form so that I can call Roles.AddUsersToRoles()?
+3
4

-, , . . :

[HttpGet]
public ActionResult Register() {

  //create an instance of your model however you are doing that
  var model = new RegisterModel();

  //pass your model instance to your view
  return View(model);
}

-, name , MVC .

  <% foreach(string role in Model.Roles) { %>
    <input type="checkbox" name="Roles" value="<%: role %>" /> <%: role %>
  <% } %>

, Roles , , , , , :

public class RegisterViewModel {
  public string[] Roles { get; set; }
  //...other properties
}

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebUI.ViewModels.RegisterViewModel>" %>

<% using (Html.BeginForm()) { %>
  <% foreach(string role in Model.Roles) { %>
    <input type="checkbox" name="Roles" value="<%: role %>" /> <%: role %>
  <% } %>

  <p><input type="submit" value="Register" /></p>
<% } %>

[HttpGet]
public ViewResult Index() {
  var model = new RegisterViewModel();
  model.Roles = System.Web.Security.Roles.GetAllRoles(); //or however you populate your roles
  return View(model);
}

[HttpPost]
public ActionResult Index(RegisterViewModel model) {
  string[] roles = model.Roles //the selected roles are here
  //....
}
+3

- ( , ):

<% foreach(string role in Model.Roles) { %>
    <input type="checkbox" name="roles" value="<%: role %>" /> <%: role %>
<% } %>

:

[HttpPost]
public ActionResult Register(string[] roles)
{
    if (ModelState.IsValid)
    {
        //save roles
        return RedirectToAction("Index", "Home");
    }

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

.

0

, .

RegisterModel. WebUI.Models.RegisterModel, .

:

public ActionResult Register() { return View(new RegisterModel()); }

:

public static string[] Roles

:

foreach(string role in RegisterModel.Roles)

0
source

For verification and sanity, I would change your model so as not to reach it, but try to capture roles directly, but rather take a list of roles in the constructor.

0
source

Source: https://habr.com/ru/post/1783536/


All Articles