简体   繁体   English

如何将部分视图呈现为字符串

[英]How to Render Partial View To String

I tried to convert Umbraco Children item into a PartialView but i want it to return as a string and not as a partial View. 我试图将Umbraco Children项目转换为PartialView,但我希望它以字符串而不是部分View的形式返回。

I read a lot in the forums and i've got errors when i'm trying these examples. 我在论坛上读了很多书,尝试这些示例时出现了错误。

I have few different templates which i want to get from the controller as the user get inside to the site. 当用户进入站点时,我想从控制器中获取几种不同的模板。 every few hours, i mean, once in few hours to check via Ajax if there is new items for this time. 我的意思是每隔几个小时要通过Ajax一次检查一次是否有新项目。 the ajax working properly. 阿贾克斯工作正常。

I get an error that say "Object reference not set to an instance of an object." 我收到一条错误消息:“对象引用未设置为对象的实例”。 and i don't really know what about is the error, is the model? 我真的不知道那是什么错误,是模型吗? is the partial name? 是部分名称吗? what's wrong? 怎么了? or how to make it work. 或如何使其运作。

Here is my code 这是我的代码

foreach (dynamic item in rangeTime.Children)
                    {
                        var docType = item.ContentType.Alias;
                        var partialViewToShow = docType.ToString().Trim().ToLower().Replace(" ", "") == "birthday" ? "BDay" : "ContentPage";

                        st.Append(string.Format("<div class=\"custom-item\" data-time-to-show=\"{0}\">", item.slideTime));
                        st.Append(RenderPartialViewToString(partialViewToShow, item));
                        st.Append("</div>");
                    }

which call to this function: (fail on "viewResult.View.Render(viewContext, sw);") 该函数的哪个调用:(在“ viewResult.View.Render(viewContext,sw);上失败”)

 protected string RenderPartialViewToString(string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = ControllerContext.RouteData.GetRequiredString("action");

        ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw); // FAIL HERE

            return sw.GetStringBuilder().ToString();
        }
    }

In this case, the HttpCompileException means that the system was unable to render the PartialView because it could not compile the razor correctly. 在这种情况下,HttpCompileException意味着系统无法渲染PartialView,因为它无法正确编译剃刀。

I suggest adding the PartialView to the page using 我建议使用将PartialView添加到页面

foreach (dynamic item in rangeTime.Children)
{
    var docType = item.ContentType.Alias;
    var partialViewToShow = docType.ToString().Trim().ToLower().Replace(" ", "") == "birthday" ? "BDay" : "ContentPage";

    Html.Partial(partialViewToShow, item)
}

to see if you can get it to render before trying to render it as a string. 在尝试将其呈现为字符串之前先查看是否可以使其呈现。 Otherwise, maybe the inner exception will have more details. 否则,内部异常可能会有更多细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM