Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:02:46+00:00 2026-06-07T15:02:46+00:00

I am trying to add and delete aspnet roles for any user in a

  • 0

I am trying to add and delete aspnet roles for any user in a MVC 3 application.

I only need to work with 3 tables which are described below.

My problems are:

  1. I need to display the user’s existing selected Roles, and other roles available
    but not selected for the user using “Checkboxes”

  2. I need to save the selected values to table aspnet_UsersInRoles table

This is what I have done so far:

  1. I have created ViewModels called AssignedRolesData.cs
  2. I have altered the models for aspnet_Users and aspnet_Users to hold the icollection navigation properties for aspnet_UsersInRoles
  3. I have created a method for the UserController Called “PopulateAssignedRoleData” to populate the existing Roles per user

My problems:

  1. I cannot get the selected roles into the checkboxes
  2. I don’t know how to save them afterwards
  3. The keys are of type GUID

Models

**using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace WWW.Models
{
    public class aspnet_Roles
    {

        public Guid ApplicationId { get; set; }
        [Key]
        public Guid RoleId { get; set; }
        public string RoleName { get; set; }
        public string LoweredRoleName { get; set; }
        public string Description { get; set; }
        public virtual ICollection<aspnet_Users> aspnet_User { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace WWW.Models
{
    public class aspnet_Users
    {
        public Guid ApplicationId { get; set; }
        [Key]
        public Guid UserId { get; set; }
        public string UserName { get; set; }
        public string LoweredUserName { get; set; }
        public string MobileAlias { get; set; }
        public bool IsAnonymous { get; set; }
        public DateTime LastActivityDate { get; set; }
        public virtual ICollection<aspnet_Roles> aspnet_Role { get; set; }
    }
}**

ViewModels

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace WWW.ViewModels
{
    public class AssignedRolesData
    {
        public Guid RoleId { get; set; }
        public string RoleName { get; set; }
        public bool Assigned { get; set; }
    }
}

UserController

   public ActionResult Edit(Guid id)
        {
            aspnet_Users aspnet_User = db.aspnet_Users
            .Include(i => i.UserId)
                //.Include(i => i.aspnet_User)
            .Where(i => i.UserId == id)
            .Single();
            PopulateAssignedRoleData(aspnet_User);
            return View(aspnet_User);

        }


 private void PopulateAssignedRoleData(aspnet_Roles aspnet_Role)
        {
            var allaspnet_Users = db.aspnet_Users;
            var UsersInRoles = new HashSet<Guid>(aspnet_Role.aspnet_User.Select(c => c.UserId));
            var viewModel = new List<AssignedRolesData>();
            foreach (var user in allaspnet_Users)
            {
                viewModel.Add(new AssignedRolesData
                {
                    RoleId = aspnet_Role.RoleId,
                    RoleName = aspnet_Role.RoleName,
                    Assigned = UsersInRoles.Contains(aspnet_Role.RoleId)
                });
            }
            ViewBag.Courses = viewModel;
        }

My Edit View

<div class="editor-field">
            <table>
                <tr>
                    @{
                        int cnt = 0;
                        List<www.ViewModels.AssignedRolesData> Roles = ViewBag.aspnet_Role;

                        foreach (var Role in Roles)
                        {
                            if (cnt++ % 3 == 0) {
                                @:  </tr> <tr> 
                            }
                            @: <td> 
                                <input type="checkbox" 
                                       name="selectedRoles" 
                                       value="@Role.RoleId" 
                                       @(Html.Raw(Role.Assigned ? "checked=\"checked\"" : "")) /> 
                                @Role.RoleId @:  @Role.RoleName
                            @:</td>
                        }
                        @: </tr>
                    }
            </table>
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

Tables

aspnet_Users Table  
ApplicationId   uniqueidentifier
UserId  uniqueidentifier
UserName    nvarchar(256)
LoweredUserName nvarchar(256)
MobileAlias nvarchar(16)
IsAnonymous bit
LastActivityDate    datetime


aspnet_Roles Table  
ApplicationId   uniqueidentifier
RoleId  uniqueidentifier
RoleName    nvarchar(256)
LoweredRoleName nvarchar(256)
Description nvarchar(256)



aspnet_UsersInRoles Table   
UserId  uniqueidentifier
RoleId  uniqueidentifier
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:02:48+00:00Added an answer on June 7, 2026 at 3:02 pm

    You need to post you view so we can see how you are trying to create the check boxes.

    To answer your second problem, you can do something like this:

    public ActionResult UpdateRoles(string userName, string[] roles)
    {
        // Remove the user from all roles, you could use more logic
        // to see what the changes are and if you need to remove a role
        // but this is just to get you started
        string[] userRoles = Roles.GetRolesForUser(user.UserName);        
        if(userRoles.Count() > 0)
        {
            foreach(string role in userRoles)
            {
                Roles.RemoveUserFromRoles(userName, role);
            }
        }
    
        // then you just add the user to the submitted roles
        foreach(string role in roles)
        {
            Roles.AddUserToRole(UserName, role);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a nested model form in which I can add/delete
I'm trying to create page where the user can add & delete soliders. I'm
i`m trying to make an NSMutableArray From NSUserDefaults so i can add/delete and edit
Am trying to add a link besides the Edit | Delete links in wordpress
I have been trying to add an askquestion dialog box to a delete button
When I'm trying to delete an external event from my calendar, if I add
I am trying to manipulate SharePoint's default calendar to add, edit and delete items
Im new to asp.net mvc. I'm trying add new model class but it got
I' trying to use Flexigrid with ASP.NET MVC. I need all the JSON type
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.