1

During AJAX call is it possible to return ViewData, TempData or a session back to the view? 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;
    }
3
  • Are you using the built-in MVC Ajax methods and scripts? If so, are you asking how to change the data in the view after executing an Ajax call? Commented Sep 4, 2010 at 5:38
  • No simply i am setting a variable in the controller , i want to retrive it in the view Commented Sep 4, 2010 at 5:42
  • Why do you need to access the viewdata/tempdata directly? Just return it as part of your JSON/XML response in your controller method. ie return JsonResult(someViewModelWhichAlsoIncludesTempData) Commented Sep 4, 2010 at 5:45

1 Answer 1

1

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.

To return an object as JSON from an ASP.NET MVC controller ActionResult, you have to do the following:

return Json(myObject); //where myObject is an object that contains all the information that you want to return.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.