在 MVC 应用程序的局部视图中访问信息

Accessing information in a partial view in an MVC application

我正在编写一个基于 C# 的 MVC 网络应用程序,它显示信息的类别和子类别。

因此,我为数据库设置了一个类别表,它与自身相关,用于子类别 - 这一切都与实体框架挂钩,并且工作完美。

我正在苦苦挣扎的是如何实施许可系统 - 我可以使用 ViewBag 轻松地做到这一点,但我想知道这是否是一种 "proper" 方法来做到这一点。

我的视图如下所示:

@using Secure_Password_Repository.Utilities;
@using Secure_Password_Repository.Settings;
@using Secure_Password_Repository.ViewModels;
@model Secure_Password_Repository.ViewModels.CategoryDisplayItem

<ul id="roottree" class="treeview ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion ui-widget ui-sortable ui-accordion-content-active">
    <li class="treeignore treeview ui-accordion ui-widget ui-helper-reset ui-sortable" data-id="0">
    <div><i class="glyphicon glyphicon-folder-open rightfolderpadding"></i>Root</div>
    <ul class="treeview ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion ui-widget ui-sortable ui-accordion-content-active" id="parent-1">
        @foreach (var categoryitem in Model.categoryListItem.SubCategories)
        {
        @Html.Partial("_CategoryItem", categoryitem)
        }
        @if (Model.CanAddCategories)
        {
        @Html.Partial("_CreateCategory", Model.categoryAddItem)
        }
    </ul>
    </li>
</ul>

CategorDisplayItem 模型如下所示:

public class CategoryDisplayItem
{
    public CategoryItem categoryListItem { get; set; }
    public CategoryAdd categoryAddItem { get; set; }
    public PasswordAdd passwordAddItem { get; set; }
    public bool CanAddCategories { get; set; }
    public bool CanEditCategories { get; set; }
}

public class CategoryItem : CategoryEdit
{
    [Required]
    public Int32 Category_ParentID { get; set; }
    public virtual ICollection<CategoryItem> SubCategories { get; set; }
    public virtual ICollection<PasswordItem> Passwords { get; set; }
    public bool CanEditCategory { get; set; }
    public bool CanDeleteCategory { get; set; }
}

public class CategoryEdit
{
    public Int32? CategoryId { get; set; }
    [Required]
    public string CategoryName { get; set; }
}

我遇到的问题是如何为 CategoryItem 视图模型设置 CanEditCategory 和 CanDeleteCategory 属性。权限不是按类别设置的,它是全局设置的,即如果我的帐户有权编辑类别,它有权编辑 所有 个类别。

CategoryItem 模型填充视图实体框架:

        Category ReturnCategoryItem = DatabaseContext.Categories
                                        .Where(c => c.CategoryId == categoryid)
                                        .Include(c => c.SubCategories)
                                        .Include(c => c.Passwords)
                                        .Include(c => c.Passwords.Select(p => p.Creator))
                                        .ToList()
                                        .Select(c => new Category()
                                        {

                                            SubCategories = c.SubCategories
                                                                //make sure only undeleted subcategories are returned
                                                                .Where(sub => !sub.Deleted)
                                                                .OrderBy(sub => sub.CategoryOrder)
                                                                .ToList(),

                                            Passwords = c.Passwords
                                                                //make sure only undeleted passwords - that the current user has acccess to - are returned
                                                                .Where(pass => !pass.Deleted && PermissionService.CanViewPassword(pass)) 
                                                                .OrderBy(pass => pass.PasswordOrder)
                                                                .ToList(),

                                            CategoryId = c.CategoryId,
                                            CategoryName = c.CategoryName,
                                            Category_ParentID = c.Category_ParentID,
                                            CategoryOrder = c.CategoryOrder,
                                            Parent_Category = c.Parent_Category,
                                            Deleted = c.Deleted
                                        })
                                        .SingleOrDefault();

SubCategories "Category" 列表隐式转换为 "CategoryItem" 列表。那么我该如何设置 CanEditCategories 和 CanDeleteCategories 属性呢?

就像我说的,我可以只使用 ViewBag...但是我读到的所有内容都说使用 ViewBag 不好。

如果这些是每个用户独有的属性,您可以将这些属性添加到您的身份中,然后在您的视图中像这样使用它们:

在您查看 cshtml 文件的顶部:

@using Microsoft.AspNet.Identity

然后使用以下语法访问您的用户名:

@if (@User.Identity.CanAddCategories())
{
  @Html.Partial("_CreateCategory", Model.categoryAddItem)
}