本文介绍了从母版Html.RenderPartial电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个情景:比方说,我有一个负责显示不同类型的内容,两个控制器的网站 - 网页和文章。我需要嵌入管窥到我的母版,将列出一些条件筛选的网页和文章,并在每一页上显示。我无法设置模型的母版我的(是吗?)。我如何使用解决这个任务Html.RenderPartial?

Here is a scenario: Let's say I have site with two controllers responsible for displaying different type of content - Pages and Articles. I need to embed Partial View into my masterpage that will list pages and articles filtered with some criteria, and be displayed on each page. I cannot set Model on my masterpage (am I right?). How do I solve this task using Html.RenderPartial?


是的,我可能会创造上市的文章和网页独立部分景色,不过,有我不能,不应设置模式母版上的一个障碍。我需要以某种方式说这里是页面作为参数传递给我的RenderPartial,也为文章。与来自masterpages数据库中的数据的RenderPartial的整个概念是一个有点模糊了我。

Yes, I'd probably create separate partial views for listing articles and pages, but still, there is a barrier that I cannot and shouldn't set model on masterpage. I need somehow to say "here are the pages" as an argument to my renderpartial, and also for articles. Entire concept of renderpartial with data from database in masterpages is a bit blurry to me.

推荐答案

如何创建的HtmlHelper扩展方法,使您可以致电控制器上的一个操作的局部视图的结果。

How about creating an HtmlHelper extension method that allows you to call a partial view result on the an action on the controller.

 public static void RenderPartialAction<TController>(this HtmlHelper helper, Func<TController, PartialViewResult> actionToRender)
    where TController : Controller, new()
{
    var arg = new TController {ControllerContext = helper.ViewContext.Controller.ControllerContext};
    actionToRender(arg).ExecuteResult(arg.ControllerContext);
}

您可以再在你的母版页使用像

you could then use this in your master page like

<% Html.RenderPartialAction((HomeController x) => x.RenderPartial()) %>

和在你的控制器适当的方法

and in your controller the appropriate method

public PartialViewResult RenderPartial()
{

     return PartialView("~/Path/or/View",_homeService.GetModel())
}

那么这是我的2美分反正

Well that is my 2 cents anyway

这篇关于从母版Html.RenderPartial电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:59