简体   繁体   English

MVC Ajax-解析表单提交返回数据

[英]MVC Ajax - Parsing Form Submission Return Data

I am using MVC Ajax to do a form submission. 我正在使用MVC Ajax进行表单提交。 What I can't get right is to get the Action on the server to return a partial view to the browser. 我无法理解的是在服务器上执行操作以将部分视图返回到浏览器。

The form submission looks like this: 表单提交如下所示:

<% using (Ajax.BeginForm("Add", "Example", new AjaxOptions { HttpMethod = FormMethod.Post.ToString(), OnComplete = "widgetAdded" } )) { %>

This hits the server and the action gets executed. 这会命中服务器,并且动作将被执行。 The JavaScript method 'widgetAdded' that gets executed after the action completes looks something like this: 操作完成后执行的JavaScript方法“ widgetAdded”看起来像这样:

function widgetAdded(ajaxContext) {
    var response = ajaxContext.get_response().get_object();
    alert(response);
}

If I return a Json result in the action like this, it works - the alert shows the data being passed from the server. 如果我在这样的操作中返回Json结果,则可以正常工作-警报显示从服务器传递的数据。

return Json("bob");

Now if I change the action to return a PartialView like this, it doesn't work. 现在,如果我更改操作以返回像这样的PartialView,它将不起作用。

return PartialView("Widgets");

I've tried to interrogate the response object in Firebug, but I can't seem to get the actual view html. 我试图询问Firebug中的响应对象,但似乎无法获取实际的html视图。 Any ideas? 有任何想法吗?

You could render your partial view to a string, and return the string: 您可以将部分视图呈现为字符串,然后返回该字符串:

        public static string RenderPartialToString(string controlName, object viewData, object model, System.Web.Routing.RequestContext viewContext)
        {

            ViewDataDictionary vd = new ViewDataDictionary(viewData);
            ViewPage vp = new ViewPage { ViewData = vd };

            vp.ViewData = vd;
            vp.ViewData.Model = model;
            vp.ViewContext = new ViewContext();
            vp.Url = new UrlHelper(viewContext);

            Control control = vp.LoadControl(controlName);

            vp.Controls.Add(control);

            StringBuilder sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
            {

                using (HtmlTextWriter tw = new HtmlTextWriter(sw))
                {

                    vp.RenderControl(tw);

                }

            }

            return sb.ToString();

        }

Code above taken from here 上面的代码是从这里获取的

To get the actual view html use: 要获取实际的html视图,请使用:

alert(ajaxContext.get_response().get_responseData());

The get_object() function works only with JSON content. get_object()函数仅适用于JSON内容。

I did some more research and it seems it's not possible to return a view as the response to a POST action. 我进行了更多研究,似乎无法返回视图作为对POST动作的响应。 I looked at the actual response being sent to the client - if I return a string (as Json) the response header actually gets populated - if I return a view the response header is empty. 我查看了发送给客户端的实际响应-如果我返回一个字符串(如Json),则响应头实际上已被填充-如果我返回一个视图,则响应头为空。

It seems like the best solution is to simply do another GET (using JQuery) and retrieving the View manually. 似乎最好的解决方案是简单地执行另一个GET(使用JQuery)并手动检索View。

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

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