1

我正在尝试为 MVC 3 应用程序中的任何用户添加和删除 aspnet 角色。

我只需要使用下面描述的 3 个表。

我的问题是:

  1. 我需要使用“复选框”显示用户现有的选定角色,以及其他可用但未为用户选择的角色

  2. 我需要将选定的值保存到表 aspnet_UsersInRoles 表中

这是我到目前为止所做的:

  1. 我创建了名为 AssignedRolesData.cs 的 ViewModel
  2. 我已经更改了 aspnet_Users 和 aspnet_Users 的模型,以保存 aspnet_UsersInRoles 的 icollection 导航属性
  3. 我为 UserController 创建了一个名为“PopulateAssignedRoleData”的方法来填充每个用户的现有角色

我的问题:

  1. 我无法将所选角色放入复选框
  2. 我不知道以后如何保存它们
  3. 键的类型为 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; }
    }
}**

视图模型

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; }
    }
}

用户控制器

   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
4

1 回答 1

1

您需要发布您的视图,以便我们可以看到您是如何尝试创建复选框的。

要回答您的第二个问题,您可以执行以下操作:

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);
    }
于 2012-07-07T10:34:13.663 回答