继承了不同视图模型的部分视图

发布于 2024-10-16 08:07:02 字数 1213 浏览 7 评论 0原文

我有一个带有 ViewModelBase 的 MVC2 项目。 我有一个由 masterPage 调用的 PartialView :

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MyProject.Web.FrontOffice.Models.ViewModels.ViewModelBase>" %>
<% Html.RenderPartial("~/Views/Shared/Controls/Header.ascx"); %>

这个 PartialView 也调用了一个 PartialView :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyProject.Web.FrontOffice.Models.ViewModels.ViewModelBase>" %>
<% Html.RenderPartial("~/Views/Shared/Controls/BannerFront.ascx"); %>

这最后一个 PartialView 需要一个继承 ViewModelBase 的 ViewModelBannerFront :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Curioos.Web.FrontOffice.Models.ViewModels.ViewModelBannerFront>" %>

我认为通过继承,不会有问题,我在控制器中设置了 ViewModelBannerFront :

public ActionResult Index(string username)
    {
        //other stuff
        ViewModelBannerFront vmbf = new ViewModelBannerFront();
        return View(vmbf);
    }

这个操作调用了一个母版页中包含的视图(其中包含标题部分视图等...)。

我有一个类型错误,你能帮我吗? 如何在最后一个 PartialView 中传递 ViewModelBannerFront? 先感谢您

I have a MVC2 project with a ViewModelBase.
I have a PartialView called by the masterPage :

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MyProject.Web.FrontOffice.Models.ViewModels.ViewModelBase>" %>
<% Html.RenderPartial("~/Views/Shared/Controls/Header.ascx"); %>

This PartialView also call a PartialView :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyProject.Web.FrontOffice.Models.ViewModels.ViewModelBase>" %>
<% Html.RenderPartial("~/Views/Shared/Controls/BannerFront.ascx"); %>

This last PartialView needs a ViewModelBannerFront that inherits ViewModelBase :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Curioos.Web.FrontOffice.Models.ViewModels.ViewModelBannerFront>" %>

I thought with the inheritance, there will be no problem, I set the ViewModelBannerFront in the controller :

public ActionResult Index(string username)
    {
        //other stuff
        ViewModelBannerFront vmbf = new ViewModelBannerFront();
        return View(vmbf);
    }

This action calls a view which is contained in the masterpage (which contains header partialview etc...).

I have a type error, could you help me please?
How can I pass a ViewModelBannerFront in my last PartialView?
Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-10-23 08:07:02

帕特,

不要运行 renderpartial,而是尝试执行以下操作,它应该可以工作:

// assuming that your banner controller is called BannerFrontController
<%Html.RenderAction("Index", "BannerFront"); %>

<% Html.RenderPartial("~/Views/Shared/Controls/BannerFront.ascx"); 的原因; %> 不起作用是因为模型是直接从父视图模型(即 MyProject.Web.FrontOffice.Models.ViewModels.ViewModelBase)传递的作为局部视图调用。这意味着您不通过控制器运行代码,而是直接调用部分并由父视图模型填充。鉴于所需的视图类型(在部分视图中)是 ViewModelBannerFront,问题就在这里。

解决该问题的另一种方法是组合视图模型,以便同时包含 ViewModelBase 和 ViewModelBannerFront。然后,只需按照以下方式调用部分视图:

// no need to call entire path as views are in shared folder
<% Html.RenderPartial("Header",  Model.ViewModelBase); %>

<% Html.RenderPartial("BannerFront", Model.ViewModelBannerFront); %>

等等,等等。希望这是有道理的。

请参阅此链接以更深入地了解 renderpartial 与 renderaction:

http://devlicio.us/blogs/derik_whittaker/archive/2008/11/24/renderpartial-vs-renderaction.aspx

Pat,

Rather than run a renderpartial, try doing the following and it should work:

// assuming that your banner controller is called BannerFrontController
<%Html.RenderAction("Index", "BannerFront"); %>

the reason that <% Html.RenderPartial("~/Views/Shared/Controls/BannerFront.ascx"); %> doesn't work is due to the fact that the model is passed directly from the parent view model (i.e. MyProject.Web.FrontOffice.Models.ViewModels.ViewModelBase) when being invoked as a partialview. This means that you don't run the code via the controller, instead, invoking the partial directly and being populated by the parent view model. Given that the view-type required (in the partialview) is ViewModelBannerFront, herein lies the issue.

The other way to attack the problem is to compose your viewmodel, so as to contain both the ViewModelBase and the ViewModelBannerFront. then, simply call the partial view along the lines of:

// no need to call entire path as views are in shared folder
<% Html.RenderPartial("Header",  Model.ViewModelBase); %>

<% Html.RenderPartial("BannerFront", Model.ViewModelBannerFront); %>

etc, etc.. hope this makes sense.

see this link to get a closer feel for renderpartial vs renderaction:

http://devlicio.us/blogs/derik_whittaker/archive/2008/11/24/renderpartial-vs-renderaction.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文