1

MVC3アプリケーションの任意のユーザーのaspnetロールを追加および削除しようとしています。

以下に説明する3つのテーブルを操作するだけで済みます。

私の問題は次のとおりです。

  1. ユーザーの既存の選択された役割、および「チェックボックス」を使用してユーザーが選択していない利用可能な他の役割を表示する必要があります

  2. 選択した値をテーブルaspnet_UsersInRolesテーブルに保存する必要があります

これは私がこれまでに行ったことです:

  1. AssignedRolesData.csというViewModelを作成しました
  2. aspnet_UsersInRolesのicollectionナビゲーションプロパティを保持するようにaspnet_Usersとaspnet_Usersのモデルを変更しました
  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
4

1 に答える 1

1

チェックボックスを作成しようとしている方法を確認できるように、ビューを投稿する必要があります。

2 番目の問題に答えるには、次のようなことができます。

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 に答える