2012-03-08 99 views
3

我有一个带有outputCache的控制器的局部视图,因为我需要缓存这个元素。有没有办法在控制器中获取PartialView HTML内容?

然后我需要在每个页面中渲染这个PartialView,但首先我需要做一些字符串替换。

所以我的问题是,我如何在控制器中获得部分视图,以便在操作内容并在将其返回给视图之前进行一些字符串替换?

感谢

回答

2

不,不要这样做。你的控制器不应该渲染你的视图,这是模板引擎的工作。

将“替换”值作为模型传递给PartialView。

public ActionResult SomeAction() 
{ 
    SomeModelmodel = new SomeModel(); // your model 
    return PartialView(model); // partial view with your model 
} 

和部分景观:

@model SomeModel 

<div>Replace your values with @Model.Value instead of String.Replace().</div> 
+0

是的,这是可行的,但在我的情况下,我需要缓存PartialView作为从另一个网站加载的外部组件。我看不到我怎么能保持它缓存,否则... – user441365 2012-03-08 16:07:43

+0

你可以通过各种参数缓存。请参阅[VaryBy属性](http://msdn.microsoft.com/zh-cn/library/system.web.mvc.outputcacheattribute.aspx)。 – jrummell 2012-03-08 16:15:10

+0

啊!这可能会有所帮助 - 我会给它一个去看 – user441365 2012-03-08 16:28:27

8

我用我的自定义Controller基地这些方法。

public string RenderPartialToString(string partialViewName, object model) 
    { 
     InvalidateControllerContext(); 
     IView view = ViewEngines.Engines.FindPartialView(ControllerContext, partialViewName).View; 
     string result = RenderViewToString(view, model); 
     return result; 
    } 

    public string RenderViewToString(string viewName, object model) 
    { 
     InvalidateControllerContext(); 
     IView view = ViewEngines.Engines.FindView(ControllerContext, viewName, null).View; 
     string result = RenderViewToString(view, model); 
     return result; 
    } 

    public string RenderViewToString(IView view, object model) 
    { 
     InvalidateControllerContext(); 
     string result = null; 
     if (view != null) 
     { 
      StringBuilder sb = new StringBuilder(); 
      using (StringWriter writer = new StringWriter(sb)) 
      { 
       ViewContext viewContext = new ViewContext(ControllerContext, view, new ViewDataDictionary(model), new TempDataDictionary(), writer); 
       view.Render(viewContext, writer); 
       writer.Flush(); 
      } 
      result = sb.ToString(); 
     } 
     return result; 
    } 

    private void InvalidateControllerContext() 
    { 
     if (ControllerContext == null) 
     { 
      ControllerContext context = new ControllerContext(System.Web.HttpContext.Current.Request.RequestContext, this); 
      ControllerContext = context; 
     } 
    } 

InvalidateControllerContext方法意味着,你需要实例Controller S IN以便手动渲染器的背景之外的谐音或意见的情况。

+0

谢谢 - 你会把这些方法放在哪里? – user441365 2012-03-08 16:13:42

+1

就像我提到的,在基本控制器 – bevacqua 2012-03-08 16:16:19

0

只是想分享一个修改@尼科的解决方案,如果你想从你的控制器动作使用ViewBag数据然后更改RenderViewToString如下,我用控制器.TempData而不是new TempDataDictionary()

public string RenderViewToString(IView view, object model) 
{ 
    InvalidateControllerContext(); 
    string result = null; 
    if (view != null) 
    { 
     StringBuilder sb = new StringBuilder(); 
     using (StringWriter writer = new StringWriter(sb)) 
     { 
      // use TempData from controller 
      ViewContext viewContext = new ViewContext(ControllerContext, view, 
       new ViewDataDictionary(model), this.TempData, writer); 
      view.Render(viewContext, writer); 
      writer.Flush(); 
     } 
     result = sb.ToString(); 
    } 
    return result; 
} 
相关问题