【问题标题】:How can the roles for individual users be listed, selected and edited with membership info如何使用会员信息列出、选择和编辑个人用户的角色
【发布时间】:2011-12-16 22:04:08
【问题描述】:

我正在尝试创建一个管理部分来管理在 vs 2010 中使用 .net MVC3 的用户。我已经弄清楚了如何分别创建和编辑新用户和角色。但是当我创建或编辑新用户时,我正在努力弄清楚如何添加角色。这是据我所知:

在我的模型中:

public class UserModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; } 
}
public class IndexViewModel
{
    public IEnumerable<UserModel> Users { get; set; }
    public IEnumerable<string> Roles { get; set; }
}

在我的控制器中

public ActionResult Index()
{        
    return View(
        new IndexViewModel
        {
            Users = Membership.GetAllUsers().Cast<MembershipUser>().Select(x => new UserModel
            {
                UserName = x.UserName,
                Email = x.Email,
            }),
            Roles = Roles.GetAllRoles()
        });
}

在视图中:

@model IEnumerable<BBmvc.Areas.Tools.Models.IndexViewModel>
//...
@foreach (var item in Model) {
 foreach (var user in item.Users)
 {
  <tr>
    <td>
        @Html.DisplayFor(modelItem => user.UserName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => user.Email)
    </td>
    <td>
        @Html.DisplayFor(modelItem => user.Password)
    </td>
    <td>
        @Html.DisplayFor(modelItem => user.ConfirmPassword)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { /* id=user.PrimaryKey */ }) |
        @Html.ActionLink("Details", "Details", new { /* id=user.PrimaryKey */ }) |
        @Html.ActionLink("Delete", "Delete", new { /* id=user.PrimaryKey */ })
    </td>
  </tr>
 }
}

我看到这个错误:

传入字典的模型项是类型 'BBmvc.Areas.Tools.Models.IndexViewModel',但是这本字典 需要“System.Collections.Generic.IEnumerable”类型的模型项

我很困惑。我在正确的轨道上吗?现在我只是试图让角色显示在页面上......最终我需要为每个用户过滤它们,以便只列出用户所在的角色。

编辑: 下面的答案解决了我遇到的视图模型问题。为每个用户显示角色的最终解决方案并没有那么复杂。我在 UserModel 中添加了一个列表:

public IEnumerable<string> UserRoles { get; set; }

在Controller中填写:

        public ActionResult Index2()
    {
        var model = Membership.GetAllUsers().Cast<MembershipUser>().Select(x => new UserModel
        {
            UserName = x.UserName,
            Email = x.Email,
            UserRoles = Roles.GetRolesForUser(x.UserName)
        });
        return View(model);
    }

然后在视图中显示:

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.UserName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Email)
    </td>
    <td>
        @foreach (var role in item.UserRoles)
        {
            @role
        }
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
        @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
        @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
    </td>
</tr>

}

谢谢大家!

【问题讨论】:

  • 你认为的“@model”是什么?
  • @model IEnumerable

标签: asp.net-mvc-3


【解决方案1】:

由于您只返回 1 个 IndexViewModel 类型的对象,您需要将您的 @model 更改为 @model BBmvc.Areas.Tools.Models.IndexViewModel

由于这个单一对象包含用户和角色列表...您需要更改视图:

 @model BBmvc.Areas.Tools.Models.IndexViewModel
 //...    
 @foreach (var user in Model.Users)
 {
          <tr>
            <td>
                @Html.DisplayFor(modelItem => user.UserName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => user.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => user.Password)
            </td>
            <td>
                @foreach (var role in Model.Roles.Where(x => x.idUser == user.idUser))
                {
                    <span>@role.Name</span> |
                }
            </td>
            <td>
                @Html.ActionLink("ViewRoles", "View Roles", new { id=user.PrimaryKey }) |
                @Html.ActionLink("Edit", "Edit", new { id=user.PrimaryKey }) |
                @Html.ActionLink("Details", "Details", new { id=user.PrimaryKey }) |
                @Html.ActionLink("Delete", "Delete", new { id=user.PrimaryKey })
            </td>
          </tr>
 }

这样,您可以获得用户的角色,然后单击“查看角色”或“设置角色”(使用最适合您的链接文本问题)您可以拥有该用户的角色列表。在那里您可以为所选用户添加/编辑/删除角色。

【讨论】:

  • 这样好多了。我如何从这里开始显示每个用户的特定角色?
  • @Rob,你可以有一个视图来显示角色,并在这个视图中放置另一个链接:'@Html.ActionLink("ViewRoles", "View Roles", new { id=user.PrimaryKey })'。除此之外,您可以在此表的其他“”中获得角色摘要。查看更新。
  • 我实际上将角色列表添加到了 UserModel 中:
【解决方案2】:

您需要更改@model

发件人:@model IEnumerable&lt;BBmvc.Areas.Tools.Models.IndexViewModel&gt;

收件人:@model BBmvc.Areas.Tools.Models.IndexViewModel

您的控制器中的代码只是返回IndexViewModel 的实例,而不是IndexViewModelIEnumerable(换句话说,集合/列表/数组/任何东西)。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签