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.

share|improve this question
feedback

3 Answers

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.

share|improve this answer
feedback

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?

share|improve this answer
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 Apr 25 '10 at 23:20
feedback

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

share|improve this answer
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 Apr 25 '10 at 23:35
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.