views:

189

answers:

2

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.

A: 

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

Darin Dimitrov
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
A: 

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?

Steve Horn
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 (http://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