[英]Calling RenderAction in Controller ASP.NET MVC 4 For Dashboard (Multi) View
我有一個模板HTML字符串,它在各個未知點都有{{Url}}個占位符,它與我的應用程序中的某些控制器/操作有關。 我需要做的是在視圖中渲染最終的html之前將html渲染到這些占位符中。
在視圖中,我可以簡單地調用Html.RenderAction("Action","Controller")
,它返回我需要的字符串。 但是 ,我需要在控制器代碼中調用此方法,例如(這是簡化的):
在“儀表板”控制器中:
var templateHtml = GetTemplateHtml();
//The following line doesn't compile
var html = Html.RenderAction("Index","PowerAnalysisDashpart");
ViewBag.Html = templateHtml.Replace("{{PowerAnalysisDashpart}}",html)
然后在“儀表板視圖”中:
<div id="content">
@Html.Raw(ViewBag.Html)
</div>
如何調用“RenderAction”來獲取控制器中的Rendered HTML字符串?
編輯:似乎對我想要實現的目標感到困惑。 基本上,我們需要管理員能夠創建一個HTML模板,它將有效地為我們的應用程序的各種不同頁面提供框架。 iframe可以工作,但我們更喜歡在服務器上構建所有HTML。
我想你想要HtmlHelper.Action
而不是RenderAction
。 RenderAction
將內容寫入響應流。 Action
將其作為字符串返回。
http://msdn.microsoft.com/en-us/library/ee721266(v=vs.108).aspx
Action在ChildActionExtensions
類中定義,該類位於System.Web.Mvc.Html
命名空間中,因此您的控制器代碼需要使用該命名空間。
Action
將Html作為字符串返回,而RenderAction
返回void,因為RenderAction
將其Html輸出直接寫入與父文檔內聯的當前響應流。 這就是它從視圖而不是控制器工作的原因。
要在MVC 4中創建HtmlHelper實例,您可以使用:
HtmlHelper helper = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView(ControllerContext, "Index"), new ViewDataDictionary(), new TempDataDictionary(), new StringWriter()), new ViewPage());
正如其他評論家在相關的SO問題上所說的那樣,這並不是真正的意圖(通過hacky方式來實現HtmlHelper顯而易見)。 我不確定它比你的替代解決方案好多了。 如果可以的話,最好重構為更多的MVC,盡管有時我意識到這不是一個選擇。
所以,找到了一個解決方案,但它非常hacky,而且必須有一個更清潔的方式!
我有一個共享的部分視圖“_view”,其中只有這個:
@{ Html.RenderAction("Index",(string)ViewBag.Controller); }
我使用了這個博客中的代碼: http ://approache.com/blog/render-any-aspnet-mvc-actionresult-to/為ActionResult類編寫擴展方法。
然后在我的控制器中我這樣做:
var templateHtml = GetTemplateHtml();
ViewBag.Controller = "PowerAnalysis";
var html = View("_view").Capture(ControllerContext);
ViewBag.Html = htmlTemplate.Replace("{{PowerAnalysis}}", html);
//Repeat for other pages
return View();
在我從博客中使用的擴展方法的代碼下面:
public class ResponseCapture : IDisposable {
private readonly HttpResponseBase response;
private readonly TextWriter originalWriter;
private StringWriter localWriter;
public ResponseCapture(HttpResponseBase response) {
this.response = response;
originalWriter = response.Output;
localWriter = new StringWriter();
response.Output = localWriter;
}
public override string ToString() {
localWriter.Flush();
return localWriter.ToString();
}
public void Dispose() {
if (localWriter != null) {
localWriter.Dispose();
localWriter = null;
response.Output = originalWriter;
}
}
}
public static class ActionResultExtensions {
public static string Capture(this ActionResult result, ControllerContext controllerContext) {
using (var it = new ResponseCapture(controllerContext.RequestContext.HttpContext.Response)) {
result.ExecuteResult(controllerContext);
return it.ToString();
}
}
}
我認為你需要局部視圖,你可以在主視圖中渲染局部視圖
@Html.Partial("myPartailView")
詳細檢查鏈接
http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC-4
要將視圖渲染為字符串,您可以遵循以下方法:
var html = RenderRazorViewToString("viewName",model); //pass model if you are binding model in view
// It will return your view as string
[NonAction]
public string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.