0

Maybe (actually I'm sure) it's me, but I cannot seem to figure out how to retrieve list items as part of a model object. The post here seems to satisfy everyone but neither answer is relatable in my limited understanding. I need to get the items that are checked so I can update the Db. Sounds simple.

My Model:

    public class UserAdminModel
{
    public Guid UserId { get; set; }
    public string UserName { get; set; }
    public List<UserRole> UserRoles { get; set; }
    public string csvAllRolls { get; set; }
}

public class UserRole
{
    public Guid RoleId { get; set; }
    public string UserRoleName { get; set; }
    public bool UserisinRole  { get; set; }
}

My View:

<% using (Html.BeginForm("UpdateRoles", "UserAdmin", FormMethod.Post))
{%>

<input type="hidden" id="UserId" name="UserId" value="<%: Model.UserId %>" />
...

<%  foreach (var role in Model.UserRoles)
    {  %>
<tr>

    <td>&nbsp;</td>
    <td colspan="2" nowrap="nowrap"><%: role.UserRoleName %></td>
    <td>&nbsp;</td>
    <td>
        <input type="checkbox" id="UserRoles" name="UserRoles" value="<%: role.UserRoleName %>"
            <% if (role.UserisinRole) { %>                
             checked="checked"
            <% } %>
             /></td>
</tr>
<%  } %>
...
        <input type="submit" name="Submit" value="Update Roles" /></td>
<%  } %>

My Controller:

        [HttpPost]
    public ActionResult UpdateAllRoles(UserAdminModel model)
    {
        Guid uid = new Guid( Request["UserId"]);


        return RedirectToAction("Index", "MyController");
    }

The UserId comes through fine but the rest of the model is null. Any help would be appreciated.

2
  • You creating checkboxes with name attributes that have no relationship at all to your model. Is the purpose of this to list all available roles and to be able to assign them to the User (by selecting checkboxes)? Commented Sep 3, 2015 at 23:26
  • yes, I want to list all roles (with check boxes), pre-select the Roles the user is already assigned to, then let the user update by checking or unchecking desired roles. The application I inherited currently has the user go into each role individually to add/remove users one at a time. I have tried many variations to setup relationship to my model to no avail, thats why I posted here. Thanx Commented Sep 4, 2015 at 12:48

1 Answer 1

1

You need to use a for loop so that your form controls have the correct name attributes to bind to your model (I'll leave it to you to convert from razor to aspx)

@using (Html.BeginForm("UpdateRoles", "UserAdmin", FormMethod.Post))
{
  @Html.HiddenFor(m => m.UserId) // or add this as a route parameter in BeginForm()
  ...
  for(int i = 0; i < Model.UserRoles.Count; i++)
  {
    @Html.HiddenFor(m => m.UserRoles[i].RoleId)
    @Html.CheckBoxFor(m => m.UserRoles[i].UserisinRole)
    @Html.LabelFor(m => m.UserRoles[i].UserisinRole, Model.UserRoles[i].UserRoleName)
  }
  <input type="submit" name="Submit" value="Update Roles" />
}

When you submit the form, model.UserRoles will contain all roles and you can get the selected roles using

var selectedRoles = model.UserRoles.Where(r => r.UserisinRole);

Side note: Using a <table> does not seem appropriate here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.