简体   繁体   English

ASP.NET MVC AJAX调用返回值以从控制器查看

[英]ASP.NET MVC AJAX call returning value to view from controller

During AJAX call is it possible to return ViewData, TempData or a session back to the view? 在AJAX调用期间,是否可以将ViewData,TempData或会话返回到视图? does these variables are included in the cycle? 这些变量是否包含在循环中? please comment 请评论

 function submitForm(frm) {
        var tdata = $(frm).serialize();

        $.ajax({
            url: "/Organization/EditOrganizationMeta",
            data: tdata,
            success: function (result) {                 
                if (result["ErrorMessage"] == "No Error") {
                    $("#" + result["DivName"] + "1").hide();
                    $("#" + result["DivName"]).show();
                    $("#" + result["DivName"]).empty();
                    $("#" + result["name"]).attr("value", result["SavedValue"]);
                    $("#" + result["DivName"]).append("<b>" + result["SavedValue"] + "</b>");
                    $("#" + result["DivName"] + "2").empty();
                    $("#" + result["DivName"] + "2").append("<b>Record is successfully saved</b>");

                }
                else if (result["ErrorMessage"] != "") {
                    $("#" + result["DivName"] + "1").show();
                    $("#" + result["DivName"]).hide();
                    $("#" + result["DivName"]).empty();
                    $("#" + result["name"]).attr("value", result["PreviousValues"]);
                    $("#" + result["DivName"] + "2").empty();
                    $("#" + result["DivName"]).append("<b>" + result["PreviousValues"] + "</b>");
                    $("#" + result["DivName"] + "2").append("<b>" + result["ErrorMessage"] + "</b>");
                }
            },
            type: "POST",
            datatype: "json"
        });

        return false;
    }

Bsaed on the JavaScript code that you just posted, I think that the best approach would be to return the parameters you want the success function to use as JSON . 基于您刚刚发布的JavaScript代码,我认为最好的方法是返回您希望success函数用作JSON的参数

To return an object as JSON from an ASP.NET MVC controller ActionResult, you have to do the following: 要从ASP.NET MVC控制器ActionResult以JSON形式返回对象,您必须执行以下操作:

return Json(myObject); //where myObject is an object that contains all the information that you want to return.

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

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