2013-10-03 99 views
1

我有一個返回我一個處理的視圖(或部分視圖)Asp.net MVC ViewEngineResult +包埋視圖

我調用此函數發送虛擬路徑視圖,像一個工作函數:

string viewHtml = GetViewPageHtml(this, model, "~/Views/PartialView.cshtml"); 

public static string GetViewPageHtml(Controller controller, object model, string viewName) 
{ 
    ViewEngineResult result = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName); 

    if (result.View == null) 
     throw new Exception(string.Format("View Page {0} was not found", viewName)); 

    controller.ViewData.Model = model; 
    StringBuilder sb = new StringBuilder(); 
    using (StringWriter sw = new StringWriter(sb)) 
    { 
     using (System.Web.UI.HtmlTextWriter output = new System.Web.UI.HtmlTextWriter(sw)) 
     { 
      ViewContext viewContext = new ViewContext(controller.ControllerContext, result.View, controller.ViewData, controller.TempData, output); 
      result.View.Render(viewContext, output); 
     } 
    } 

    return sb.ToString(); 
} 

有沒有解決方案使這個功能找到嵌入的意見?

如果我有一個嵌入視圖,並調用同一個函數,它不會發現頁面:

// not working 
string viewHtml = GetViewPageHtml(this, model, "Application1.Views.EmbededPartialView.cshtml"); 

回答