我正在尝试将部分视图呈现为字符串以在 JSON 对象内返回。我有这个代码来做到这一点:
/// <summary>
/// Render razor view to a string.
/// </summary>
/// <param name="model">Model sent to view.</param>
/// <param name="filePath">Path to the view location in project hierarchy.</param>
/// <returns>String with the html code of returned view.</returns>
public static string Func_GetRazorViewAsString(object model, string filePath)
{
var st = new StringWriter();
var context = new HttpContextWrapper(HttpContext.Current);
var routeData = new RouteData();
var controllerContext = new ControllerContext(new RequestContext(context, routeData), new FakeController());
var razor = new RazorView(controllerContext, filePath, null, false, null);
razor.Render(new ViewContext(controllerContext, razor, new ViewDataDictionary(model), new TempDataDictionary(), st), st);
return st.ToString();
}
当 View 不包含 PartialView 时它工作得很好,但是当要呈现的视图调用另一个 Partial View 时它会抛出异常。例如:
@using Anpr_Web_Manager.App_GlobalResources
@model IEnumerable<Anpr_Web_Manager.Models.DetectionCardViewModel>
@{
int iCountElementsRow = 0;
int iNumberElements = Model.Count();
int iNumNavigationPages = (int)Math.Ceiling(iNumberElements / (double)3);
}
<div class="col-12 bootstrap-carousel">
<div id="carouselDetections" class="carousel slide" data-ride="carousel" data-interval="false">
@if (iNumberElements != 0)
{
@:<ol class="carousel-indicators">
<li data-target="#carouselDetections" data-slide-to="0" class="active"></li>
for (int i = 1; i < iNumNavigationPages; i++)
{
<li data-target="#carouselDetections" data-slide-to="@i"></li>
}
@:</ol>
}
异常消息是:
“RouteData 必须包含一个名为 'controller' 且具有非空字符串值的项目。”
请问,我该如何解决?我知道我可以复制和粘贴第二个 Partial View 的代码,但这不是我最好的解决方案,因为我在其他地方使用了这个 Partial View,我不想重复相同的代码。
SMILET
相关分类