异常将部分视图呈现为包含另一个部分视图的字符串

时间:2018-08-20 16:10:12

标签: c# asp.net-mvc razor

我正在尝试将部分视图呈现为字符串以返回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>
            }
            <div class="container carousel-inner">
                <div class="row row-equal row-slider carousel-item active">
                    @foreach (var item in Model)
                    {
                        if (iCountElementsRow != 0 && iCountElementsRow % 3 == 0)
                        {
                            //Insert new row
                            @:</div>
                            @:<div class="row row-equal row-slider carousel-item">
                        }
                        //TODO: RENDER ELEMENT
                        @Html.Partial("~/Views/Capturas/_DetectionElement.cshtml", item);
                        iCountElementsRow++;
                    }
                </div>
            </div>
        </div>
    </div>

异常消息是:

“ RouteData必须包含一个带有非空字符串值的名为“ controller”的项目。”

请,我该如何解决?我知道我可以复制并粘贴第二个“局部视图”的代码,但这不是我的最佳解决方案,因为我在其他地方使用了该“局部视图”,并且我不想重复相同的代码。

非常感谢。最好的问候,

1 个答案:

答案 0 :(得分:1)

使用此方法将View呈现为字符串:

public static string RenderViewToString(ControllerContext context, string viewPath, object viewModel = null, bool partial = false)
{
    // get the ViewEngine for this view
    ViewEngineResult viewEngineResult = null;
    if (partial)
        viewEngineResult = ViewEngines.Engines.FindPartialView(context, viewPath);
    else
        viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);

    if (viewEngineResult == null)
        throw new FileNotFoundException("View cannot be found.");

    // get the view and attach the model to view data
    var view = viewEngineResult.View;
    context.Controller.ViewData.Model = viewModel;

    string result = null;

    using (var sw = new StringWriter())
    {
        var ctx = new ViewContext(context, view, context.Controller.ViewData, context.Controller.TempData, sw);
        view.Render(ctx, sw);
        result = sw.ToString();
    }

    return result;
}

可以通过以下方式进行呼叫:

@foreach (var item in Model.SomeIEnumerableModel)
{
    @Html.Partial("~/Views/SomePath/SomeViewTwo.cshtml", item);
}

在“父部分视图”中。

可以使用以下方法在Controller中调用它(假设您从ajax请求返回json,并且RenderViewToString方法位于调用方Controller中):

public ActionResult TestViewToString()
{
    var viewModel = new TestViewModel();
    // Populate ViewModel here ...

    string data = RenderViewToString(ControllerContext, "~/Views/SomePath/SomeViewOne.cshtml", viewModel, true);
    return Json(data, JsonRequestBehavior.AllowGet);
}