1

All the examples of json I can find online only show how to submit json arrays w/ the jquery command $.ajax(). I'm submitting some data from a custom user control as a json array. I was wondering if it's possible to submit a json array as a regular post request to the server (like a normal form) so the browser renders the page returned.

Controller:

[JsonFilter(Param = "record", JsonDataType = typeof(TitleViewModel))]
public ActionResult SaveTitle(TitleViewModel record)
{
    // save the title.
    return RedirectToAction("Index", new { titleId = tid });
}

Javascript:

function SaveTitle() {
    var titledata = GetData();

    $.ajax({
        url: "/Listing/SaveTitle",
        type: "POST",
        data: titledata,
        contentType: "application/json; charset=utf-8",
     });

}

Which is called from a save button. Everything works fine but the browser stays on the page after submitting. I was thinking of returning some kind of custom xml from the server and do javascript redirect but it seems like a very hacky way of doing things. Any help would be greatly appreciated.

3 Answers 3

2

This is an old question but this might be useful for anyone finding it --

You could return a JsonResult with your new titleId from the web page

public ActionResult SaveTitle(TitleViewModel record) {
     string tId = //save the title 
     return Json(tId)
}

and then on your ajax request add a success function:

function SaveTitle() {
    var titledata = GetData();

    $.ajax({
        url: "/Listing/SaveTitle",
        type: "POST",
        data: titledata,
        contentType: "application/json; charset=utf-8",
        success: function(data) { window.location = "/Listing/Index?titleId=" + data; }
     });

}

That would redirect the page after a successful ajax request. I just saw that you mentioned this at the end of your post but I think it is an easy and quick way of getting around the issue.

0

Phil Haack has a nice post discussing this scenario and shows the usage of a custom value provider instead of an action filter.

1
  • Interesting article but I'm not having trouble getting the data to the server, it works fine using an action filter. My problem is with the response that returns from the server and how it is handled by the browser.
    – j3ko
    Commented Apr 25, 2010 at 23:35
0

I don't understand why you would want to post Json if you're wanting to do a full page post. Why not just post normal form variables from the Html form element?

1
  • Its because I'm not using normal form elements for some of the data I'm trying to post back. I'm using a list tree type control (interface.eyecon.ro/demos/drag_drop_tree.html) which is modified via javascript on the browser and I want to post back the tree structure to the server.
    – j3ko
    Commented Apr 25, 2010 at 23:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.