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:
-
I need to display the user’s existing selected Roles, and other roles available
but not selected for the user using “Checkboxes” -
I need to save the selected values to table aspnet_UsersInRoles table
This is what I have done so far:
- I have created ViewModels called AssignedRolesData.cs
- I have altered the models for aspnet_Users and aspnet_Users to hold the icollection navigation properties for aspnet_UsersInRoles
- I have created a method for the UserController Called “PopulateAssignedRoleData” to populate the existing Roles per user
My problems:
- I cannot get the selected roles into the checkboxes
- I don’t know how to save them afterwards
- 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
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: