簡體   English   中英

用戶和角色成表asp.net MVC

[英]User & roles into a table asp.net mvc

我使這件事正常工作,但是我想采取一種不同的方法,因為當用戶只具有一個角色時,表格丟失了行。 我在網站上獲得了3個角色:用戶,超級用戶和管理員。

這是我的控制器:

 public ActionResult UserList()
 {
    var userRoles = new List<RolesViewModel>();
    var context = new ApplicationDbContext();
    var userStore = new UserStore<ApplicationUser>(context);
    var userManager = new UserManager<ApplicationUser>(userStore);


    foreach (var user in userStore.Users)
    {
        var r = new RolesViewModel
        {
            UserName = user.UserName
        };
        userRoles.Add(r);
    }

    foreach (var user in userRoles)
    {
        user.RoleNames = userManager.GetRoles(userStore.Users.First(s => s.UserName == user.UserName).Id);
    }

    return View(userRoles);
}

這是我的RolesViewModel:

public class RolesViewModel
{
    public IEnumerable<string> RoleNames { get; set; }
    public string UserName { get; set; }
}

和視圖:

<table class="table table-hover">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.UserName)
        </th>
        <th>
            Role 1
        </th>
        <th>
            Role 2
        </th>
        <th>
            Role 3
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.UserName)
            </td>

            @foreach (var role in item.RoleNames)
            {               
                <td>
                    @Html.DisplayFor(modelItem => role)
                </td>
            }

        </tr>
    }
</table>

因此,在我獲得角色1,角色2和角色3的表中,我想讓用戶超級用戶和admin分別具有一個模型,但是我無法真正使其工作,請問如何才能做到這一點?

我了解您期望擔任三個特定角色,但是您的模型將來可能會包含更多角色。 您的介紹在這方面不是很前瞻性的考慮。 而且,這種結構分解出簡潔的答案的方式太多了。 相反,我建議您重組網格和視圖模型以適應。

稍微更改視圖模型:

public class RolesViewModel
{
    public string RoleNames { get; set; }
    public string UserName { get; set; }
}

接下來,將使您的數據訪問更加高效,並修改了Roles屬性值的構造方式:

public ActionResult UserList() {
    var userRoles = new List<RolesViewModel>();
    var context = new ApplicationDbContext();
    var userStore = new UserStore<ApplicationUser>(context);
    var userManager = new UserManager<ApplicationUser>(userStore);


    foreach (var user in userStore.Users)
    {
        var r = new RolesViewModel()
        {
            UserName = user.UserName,
            RoleNames = string.Join(",", userManager.GetRoles(user.Id))
        };
        userRoles.Add(r);
    }

    return View(userRoles); 
}

然后最后簡化您的網格:

<table class="table table-hover">
<tr>
    <th>
        User Name
    </th>
    <th>
        Roles
    </th>
</tr>
@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(m => item.UserName)
        </td>

        <td>
            @Html.DisplayFor(m => item.RoleNames)
        </td>
    </tr>
}
</table>

最后,如果您必須堅持使用此結構,那么以下是至少使用復選框樣式設置的相同演示:

public class RolesViewModel
{
    public string UserName { get; set; }
    public bool UserRole { get; set; }
    public bool SuperUserRole { get; set; }
    public bool AdminRole { get; set; }
}

public ActionResult UserList()
{
    var userRoles = new List<RolesViewModel>();
    var context = new ApplicationDbContext();
    var userStore = new UserStore<ApplicationUser>(context);
    var userManager = new UserManager<ApplicationUser>(userStore);


    foreach (var user in userStore.Users)
    {
        var roles = userManager.GetRoles(user.Id);
        var r = new RolesViewModel()
        {
            UserName = user.UserName,
            UserRole = roles.Contains("User"),
            SuperUserRole = roles.Contains("SuperUser"),
            AdminRole = roles.Contains("Admin")
        };
        userRoles.Add(r);
    }

    return View(userRoles);
}
    }
}

<table class="table table-hover">
<tr>
    <th>
        User Name
    </th>
    <th>
        User
    </th>
    <th>
        Super User
    </th>
    <th>
        Admin
    </th>
</tr>
@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(m => item.UserName)
        </td>
        <td>
            @Html.CheckBoxFor(m => item.UserRole)
        </td>
        <td>
            @Html.CheckBoxFor(m => item.SuperUserRole)
        </td>
        <td>
            @Html.CheckBoxFor(m => item.AdminRole)
        </td>
    </tr>
}
</table>

記錄下來,我意識到該控制器在如何通過為每個唯一用戶進行一系列往返數據存儲的方式來抓住每個用戶角色方面有些丑陋。 如果將其使用,我會將其留給OP進行清理...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM