渲染到字符串MVC4局部视图 [英] Render partial view to string MVC4
本文介绍了渲染到字符串MVC4局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下渲染的局部视图为一个字符串...
保护字符串RenderPartialViewToString(字符串的viewName,对象模型)
{
如果(string.IsNullOrEmpty(的viewName))
的viewName = ControllerContext.RouteData.GetRequiredString(行动); ViewData.Model =模型; 使用(VAR SW =新的StringWriter())
{
ViewEngineResult的ViewResult = ViewEngines.Engines.FindPartialView(ControllerContext,的viewName);
VAR viewContext =新ViewContext(ControllerContext,viewResult.View,ViewData的,TempData的,SW);
viewResult.View.Render(viewContext,SW); 返回sw.GetStringBuilder()的ToString()。
}
}
然而,它返回这样奇怪的下方标签HTML ...(我已经包含一小部分作为它的一个很大的视图)
< $ A $>< DIV< / $ A $>< $ B $>类=模式隐藏变脸< / $ B $>< $ C $> ID =对话< / $ C $>< $ D $>
这发生在整个HTML。这部分应该是这样的......
< DIV CLASS =模式隐藏变脸ID =对话的风格=显示:无;>
解决方案
奇怪,经过清理和重建它固定的问题,必须是VS小鬼。
I am using the following to render a partial view to a string...
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
However it returns the html with strange tags like this below... (I have included a small section as its a big view)
<$A$><div</$A$><$B$> class="modal hide fade"</$B$><$C$> id="dialog"</$C$><$D$>
This happens throughout the HTML. This section should look like this...
<div class="modal hide fade" id="dialog" style="display: none;">
解决方案
Strange, after a Clean and Rebuild it fixed the issue, must be a VS gremlin.
这篇关于渲染到字符串MVC4局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文