[英]Render PartialView Into String MVC4 Using Razor
how to render partial views to strings within controllers when using razor view engine with mvc4 在带有Mvc4的razor视图引擎中,如何在控制器内的字符串上呈现部分视图
With webView (Aspx) i used : 使用webView(Aspx)我使用了:
public static string RenderPartialView(this Controller controller, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = controller.ControllerContext.RouteData.GetRequiredString("action");
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
I tried that solution with razor , but it can't resolve the view, Error Message : 我用razor尝试了该解决方案,但无法解决该视图,错误消息:
The View can't be null => ViewResult.View View不能为null => ViewResult.View
The call to FindPartialView will not throw an exception if viewName cannot be found but the viewResult.View property will be null. 如果找不到viewName,但是对viewPart.View属性为null,则对FindPartialView的调用不会引发异常。
Also, when the view cannot be found, the viewResult.SearchedLocations will contain the list of locations where the view was searched. 另外,当找不到视图时,viewResult.SearchedLocations将包含搜索视图的位置列表。 This will show you where your view was searched for. 这将向您显示搜索视图的位置。
So, given that there's not much information in your original question (and I can't comment with extra questions), my best bet would be that the viewName you're passing in cannot be found by any of the view engines. 因此,鉴于您的原始问题中没有太多信息(并且我无法用其他问题发表评论),我最好的选择是,任何视图引擎都找不到您要传递的viewName。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.