继承了不同视图模型的部分视图
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
帕特,
不要运行 renderpartial,而是尝试执行以下操作,它应该可以工作:
<% Html.RenderPartial("~/Views/Shared/Controls/BannerFront.ascx"); 的原因; %>
不起作用是因为模型是直接从父视图模型(即MyProject.Web.FrontOffice.Models.ViewModels.ViewModelBase
)传递的作为局部视图调用。这意味着您不通过控制器运行代码,而是直接调用部分并由父视图模型填充。鉴于所需的视图类型(在部分视图中)是 ViewModelBannerFront,问题就在这里。解决该问题的另一种方法是组合视图模型,以便同时包含 ViewModelBase 和 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:
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) isViewModelBannerFront
, 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:
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