2012-07-07 101 views
1

我想爲MVC 3應用程序中的任何用戶添加和刪除aspnet角色。在MVC 3應用程序中爲用戶管理角色

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

我的問題是:

  1. 我需要顯示用戶的當前選擇的角色,並選擇使用「複選框」

  2. 我需要給用戶提供 而不是其他角色將所選值保存到表aspnet_UsersInRoles表

這是我迄今所做的:

  1. 我已經創建的ViewModels稱爲AssignedRolesData.cs
  2. 我已經改變了aspnet_Users和aspnet_Users模型持有ICollection的導航屬性aspnet_UsersInRoles
  3. 我有創建了一個方法名爲「PopulateAssignedRoleData」的UserController中來填充每個用戶的現有角色

我的問題:

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

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 
+0

你設法得到它的工作?當你說,「我無法將選定的角色放入複選框」,你的意思是你不能讓選定的角色顯示爲勾選?它可以幫助你,如果你看看呈現的HTML。在Google Chrome瀏覽器中,右鍵單擊並點擊「查看源代碼」。 – 2012-07-08 12:08:28

回答

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); 
    } 
+0

我剛剛發佈了我的觀點 – MataHari 2012-07-07 14:44:11

相關問題