Asp.net mvc 3 MVC3-不是控制器中方法的渲染视图
我不知道如何最好地描述我的需求,但我还是这样说。我正在尝试从应用程序中的以下控制器/模型渲染视图: CustomerController.cs代码段:Asp.net mvc 3 MVC3-不是控制器中方法的渲染视图,asp.net-mvc-3,partial,nopcommerce,Asp.net Mvc 3,Partial,Nopcommerce,我不知道如何最好地描述我的需求,但我还是这样说。我正在尝试从应用程序中的以下控制器/模型渲染视图: CustomerController.cs代码段: [NonAction] protected CustomerNavigationModel GetCustomerNavigationModel(Customer customer) { var model = new CustomerNavigationModel(); model.HideAvatar = !_customerSe
[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>}
@model CustomerNavigationModel
@使用Nop.Web.Models.Customer;
@如果(!Model.HideInfo)
{
}
观点:
@Html.Partial(“MyAccountNavigation”,Model.NavigationModel,new ViewDataDictionary())
我知道它无法呈现MyAccountNavigation,因为它在控制器中不存在。但是,取决于语法所在的页面,它可以工作。那么,有没有一种方法可以在不改变控制器代码的情况下实现这一点?提前感谢。我从您的问题中了解到,您需要从多个页面执行“MyAccountNavigation”部分视图,为此,您可以将此部分视图放在共享文件夹中 如果我错了,请纠正我 编辑的文本 基本控制器: 例外核心法 您可以将菜单或该型号存储在ViewBag中,如
var customerNavigationModel = assigned your value;
ViewBag.MenuData = customerNavigationModel;
从视野
@{Html.RenderPartial("MyAccountNavigation", ViewBag.MenuData);}
MVC按特定顺序查找局部视图。要使部分视图正常工作,您需要将
MyAccountNavigation.cshtml'放入
~/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
感兴趣
这解释了为什么当您尝试在与客户相关的视图中使用视图时,会发现该视图,而在其他任何地方都不起作用。您的局部视图位于何处?有效的视图,它们位于何处?不起作用的视图,它们位于哪里?部分视图文件是~/Views/Customer/MyAccountNavigation.cshtml,起作用的视图位于~/Views/Customer/文件夹中。如果我在~/Views/Shared/中的任何文件中插入“@Html.Partial(“MyAccountNavigation”,Model.NavigationModel,new ViewDataDictionary()”),它将不起作用。我已将MyAccountNavigation.cshtml移动到共享文件夹中,并将语法更改为:@Html.Partial(“MyAccountNavigation”,new ViewDataDictionary(this.ViewData)),但返回错误:对象引用未设置为对象的实例。第18行:@if(!Model.HideInfo)。这是由于模型为空而产生的错误。您需要先检查模型!=空,然后检查HideInfo的值。如何从“受保护的CustomerNavigationModel GetCustomerNavigationModel(Customer)”查看模型?我尝试了“@Html.Partial(“~/Views/Customer/MyAccountNavigation.cshtml”,new Nop.Web.Models.Customer.CustomerNavigationModel()”),但属性没有传递到模型中。我只想在可能的情况下更改视图中的语法。@scoo-b。我可以说,您希望在该部分视图中传递CustomerNavigationModel。为此,您需要将CustomerNavigationModel存储在控制器的ViewBag中,并从视图中获取相同的值。检查我编辑的代码。