Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

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 herehere 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.

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.

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.

Source Link
Darkloki
  • 686
  • 1
  • 13
  • 31

MVC 3 Retrieve checkbox values from View, Model

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.