AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

ASP.NET MVC Multiple check box

models

public class UserRoleViewModel
{
    public UserRoleViewModel()
    {
        Roles = new List<RoleViewModel>();
    }
    public string UserId { get; set; }
    public List<RoleViewModel> Roles { get; set; }
}

public class RoleViewModel
{
    public bool IsInRole { get; set; }
    [HiddenInput(DisplayValue = false)]
    public int RoleId { get; set; }
    [HiddenInput(DisplayValue = true)]
    public string RoleName { get; set; }
}

controller

public ActionResult EditUserRole(string userId)
{
    var user = userService.GetUser(userId);
    var roles = userService.GetRoles();

    UserRoleViewModel viewModel = new UserRoleViewModel();
    viewModel.UserId = user.UserId;
    //Match the roles the user is in with the list of roles
    foreach (var role in roles)
    {
        viewModel.Roles.Add(new RoleViewModel
                            {
                                IsInRole = user.Roles.Any(r => r.RoleId == role.RoleId),
                                RoleId = role.RoleId,
                                RoleName = role.RoleName
                            });
    }

    return View(viewModel);
}

[HttpPost]
public ActionResult EditUserRole(UserRoleViewModel model)
{
    List<Role> roles = model.Roles.Where(r => r.IsInRole).Select(r => new Role {RoleId = r.RoleId, RoleName = r.RoleName}).ToList();
    userService.AddRolesToUser(model.UserId, roles);

    return View();            
}

view (EditRole.cshtml)

@model WebUI.ViewModel.UserRoleViewModel

@using (Html.BeginForm("EditUserRole", "Administrator"))
{
    @Html.HiddenFor(x => Model.UserId)

    @Html.EditorFor(x => Model.Roles)

    <input type="submit" />
}

And editor template

@model WebUI.ViewModel.RoleViewModel

@Html.CheckBoxFor(m => m.IsInRole, new { onclick="this.form.submit();"})
@Html.HiddenFor(m => m.RoleId)
@Html.LabelFor(m => m.IsInRole, Model.RoleName)
<br />

Important line that is in editor template (I added onclick event):

@Html.CheckBoxFor(m => m.IsInRole, new { onclick="this.form.submit();"})

Everything is fine, but only roleNames are posting with null value. I mean, I debugged it and IsInRole and RoleId are expected, but only roleNames are null.

I cant find the solution. Any suggestion about this? Why only roleNames are posting null...

Thanks...

Upvotes: 0

Views: 2890

Answers (2)

Nwafor
Nwafor

Reputation: 184

I found that there a lot of things in the Html.CheckBoxFor() helper and how it works. please, check this post: ASP.NET MVC Multiple Checkboxes Validation and Handling using C#

Upvotes: 0

PSL
PSL

Reputation: 123739

It is because roleName is not an input Field. Only input fields are posted back to the server not the display fields.

So you could store role name in a hidden field like RoleId and you should be able to access it.

<input type="hidden" id="hdnRoleName" name="hdnRoleName" value="@Model.RoleName" />

And in your action you can access it from Request["hdnRoleName"] or rather from formcollection.

Upvotes: 1

Related Questions