0

我不知道如何最好地描述我的要求,但这里有。我正在尝试在nopCommerce应用程序中从以下控制器/模型呈现视图:

CustomerController.cs 片段:

[NonAction]
protected CustomerNavigationModel GetCustomerNavigationModel(Customer customer)
{
   var model = new CustomerNavigationModel();
   model.HideAvatar = !_customerSettings.AllowCustomersToUploadAvatars;
   model.HideRewardPoints = !_rewardPointsSettings.Enabled;
   model.HideForumSubscriptions = !_forumSettings.ForumsEnabled || !_forumSettings.AllowCustomersToManageSubscriptions;
   model.HideReturnRequests = !_orderSettings.ReturnRequestsEnabled || _orderService.SearchReturnRequests(customer.Id, 0, null).Count == 0;
   model.HideDownloadableProducts = _customerSettings.HideDownloadableProductsTab;
   model.HideBackInStockSubscriptions = _customerSettings.HideBackInStockSubscriptionsTab;
   return model;
}

CustomerNavigationModel.cs:

public partial class CustomerNavigationModel : BaseNopModel
{
    public bool HideInfo { get; set; }
    public bool HideAddresses { get; set; }
    public bool HideOrders { get; set; }
    public bool HideBackInStockSubscriptions { get; set; }
    public bool HideReturnRequests { get; set; }
    public bool HideDownloadableProducts { get; set; }
    public bool HideRewardPoints { get; set; }
    public bool HideChangePassword { get; set; }
    public bool HideAvatar { get; set; }
    public bool HideForumSubscriptions { get; set; }

    public CustomerNavigationEnum SelectedTab { get; set; }
 }

public enum CustomerNavigationEnum
{
    Info,
    Addresses,
    Orders,
    BackInStockSubscriptions,
    ReturnRequests,
    DownloadableProducts,
    RewardPoints,
    ChangePassword,
    Avatar,
    ForumSubscriptions
}

MyAccountNavigation.cshtml 片段:

 @model CustomerNavigationModel
 @using Nop.Web.Models.Customer;
 @if (!Model.HideInfo)
 {
      <li><a href="@Url.RouteUrl("CustomerInfo")" class="@if (Model.SelectedTab == CustomerNavigationEnum.Info)
          {<text>active</text>}
          else
          {<text>inactive</text>}">@T("Account.CustomerInfo")</a></li>}

视图:@Html.Partial("MyAccountNavigation", Model.NavigationModel, new ViewDataDictionary())

我知道它无法呈现 MyAccountNavigation 因为它在控制器中不存在。但是,取决于放置语法的页面,它会起作用。那么有没有办法在不更改控制器中的代码的情况下实现这一点?提前致谢。

4

2 回答 2

0

MVC 以特定顺序查找局部视图。为了使您的局部视图起作用,您需要放置MyAccountNavigation.cshtml' into~/Views/Shared/`

我查看了源代码,这就是发生的事情:

 public RazorViewEngine(IViewPageActivator viewPageActivator)
            : base(viewPageActivator)
        {
            AreaViewLocationFormats = new[]
            {
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/{1}/{0}.vbhtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml",
                "~/Areas/{2}/Views/Shared/{0}.vbhtml"
            };
            AreaMasterLocationFormats = new[]
            {
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/{1}/{0}.vbhtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml",
                "~/Areas/{2}/Views/Shared/{0}.vbhtml"
            };
            AreaPartialViewLocationFormats = new[]
            {
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/{1}/{0}.vbhtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml",
                "~/Areas/{2}/Views/Shared/{0}.vbhtml"
            };

            ViewLocationFormats = new[]
            {
                "~/Views/{1}/{0}.cshtml",
                "~/Views/{1}/{0}.vbhtml",
                "~/Views/Shared/{0}.cshtml",
                "~/Views/Shared/{0}.vbhtml"
            };
            MasterLocationFormats = new[]
            {
                "~/Views/{1}/{0}.cshtml",
                "~/Views/{1}/{0}.vbhtml",
                "~/Views/Shared/{0}.cshtml",
                "~/Views/Shared/{0}.vbhtml"
            };
            PartialViewLocationFormats = new[]
            {
                "~/Views/{1}/{0}.cshtml",
                "~/Views/{1}/{0}.vbhtml",
                "~/Views/Shared/{0}.cshtml",
                "~/Views/Shared/{0}.vbhtml"
            };

            FileExtensions = new[]
            {
                "cshtml",
                "vbhtml",
            };
        }

我们对AreaPartialViewLocationFormats和感兴趣PartialViewLocationFormats

这解释了为什么当您尝试在与客户相关的视图中使用它时会找到您的视图,而它在其他任何地方都不起作用。

于 2012-09-11T07:59:34.690 回答
0

无论我从您的问题中了解到什么,您都需要从许多页面执行“MyAccountNavigation”部分视图,为此您可以将此部分视图放在共享文件夹中。

如果我错了,请纠正我。

编辑文本

基础控制器:

ExecuteCore 方法

您可以将菜单或该模型存储在 ViewBag 中,例如

var customerNavigationModel = assigned your value;
ViewBag.MenuData = customerNavigationModel;

从视图

@{Html.RenderPartial("MyAccountNavigation", ViewBag.MenuData);}
于 2012-09-10T10:32:05.110 回答