javascript ASP.NET MVC 将部分视图呈现为字符串以使用 JSON 返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15588488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:19:04  来源:igfitidea点击:

ASP.NET MVC render partial view to a string to return with JSON

javascriptc#jqueryasp.net-mvcasp.net-mvc-4

提问by Patrick

So i have a finished method that works and i use it all over the website:

所以我有一个完整的方法,我在整个网站上使用它:

public PartialViewResult GetBlogEntries(int itemsToTake = 5)
{
    ...
    return PartialView("_BlogPost", model);
}

Now i want to get this from my javascript in JSON form.

现在我想以 JSON 形式从我的 javascript 中获取它。

public JsonResult GetBlogPostJson()
{      
    var blogEntry = GetBlogEntries(1);
    var lastEntryId = GetLastBlogEntryId();
    return Json(new {Html = blogEntry, LastEntryId = lastEntryId}, JsonRequestBehavior.AllowGet);
}

Idea is to get it like this:

想法是这样得到它:

$.ajax({
            url: '/Blog/GetBlogPostJson',
            dataType: 'json',
            success: function (data) {
                var lastEntryId = data.LastEntryId;
                var html = data.Html;
                ...
            }
        }); 

Problem is that this of course do not produce a string, but a PartialViewResult.

问题是这当然不会产生字符串,而是产生 PartialViewResult。

Question is, how can i resolve the PartialViewResult into html that i can send back with JSON ?

问题是,我如何将 PartialViewResult 解析为可以用 JSON 发回的 html?

回答by Dave Alperovich

I went through this about 6 months ago. Goal was to use a partial to populate a jquery popup dialog.

大约 6 个月前,我经历了这个过程。目标是使用部分来填充 jquery 弹出对话框。

The problem is the View Engine wants to Render them in it's own awkward order...

问题是视图引擎想要以它自己尴尬的顺序渲染它们......

Try this. LMK if it needs clarification.

试试这个。如果需要澄清,LMK。

    public static string RenderPartialViewToString(Controller thisController, string viewName, object model)
    {
        // assign the model of the controller from which this method was called to the instance of the passed controller (a new instance, by the way)
        thisController.ViewData.Model = model;

        // initialize a string builder
        using (StringWriter sw = new StringWriter())
        {
            // find and load the view or partial view, pass it through the controller factory
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(thisController.ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(thisController.ControllerContext, viewResult.View, thisController.ViewData, thisController.TempData, sw);

            // render it
            viewResult.View.Render(viewContext, sw);

            //return the razorized view/partial-view as a string
            return sw.ToString();
        }
    }