1
我想为MVC 3应用程序中的任何用户添加和删除aspnet角色。在MVC 3应用程序中为用户管理角色
我只需要使用下面描述的3个表格。
我的问题是:
我需要显示用户的当前选择的角色,并选择使用“复选框”
我需要给用户提供 而不是其他角色将所选值保存到表aspnet_UsersInRoles表
这是我迄今所做的:
- 我已经创建的ViewModels称为AssignedRolesData.cs
- 我已经改变了aspnet_Users和aspnet_Users模型持有ICollection的导航属性aspnet_UsersInRoles
- 我有创建了一个方法名为“PopulateAssignedRoleData”的UserController中来填充每个用户的现有角色
我的问题:
- 我不能让所选择的角色进入复选框
- 我不知道如何事后拯救他们
- 的关键是类GUID的
模式
**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;
}
我的编辑,查看
<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>
表
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
你设法得到它的工作?当你说,“我无法将选定的角色放入复选框”,你的意思是你不能让选定的角色显示为勾选?它可以帮助你,如果你看看呈现的HTML。在Google Chrome浏览器中,右键单击并点击“查看源代码”。 – 2012-07-08 12:08:28