0

I was using some old example for MVC2, that used a [fromJson] attribute when posting json to a controller action, it worked fine except that it did not catch model errors, so ModelState.IsValid is always true. Then I saw that posting JSON was build into MVC3 so I have upgraded my code. But now I have another issue :) using:

ko.utils.postJson(location.href, json); 

The binding is not working, and my model is empty.

But if I use JQuery:

$.ajax({
                            url: '@Url.Action("Create")',
                            contentType: 'application/json; charset=utf-8',
                            type: "POST",
                            data: json,
                            dataType: 'json',
                            success: function(result) {
                                alert("yay");
                            },
                            error: function(xhr, ajaxOptions, thrownError) {
                                alert(xhr.statusText + " " + xhr.responseText);
                            }
                        });

So I think i will move my ko.utils.postJson to JQuery $.ajax but how do I just post so I can do a RedirectToAction afterwards?

So to clarify! the controller action looks like this:

public ActionResult Create(QuestionViewModel questionViewModel){

    if (ModelState.IsValid)
    {
        questionViewModel.Save();

        TempData.Add(Config.MODEL, questionViewModel);
        return RedirectToAction("Edit");
    }

    PopulateViewBag();

    return View(questionViewModel);
}

I have found this http://groups.google.com/group/knockoutjs/browse_thread/thread/e631a544de2ad51e in the Knockout forum so ko.utils.postJson is a "normal" form submit. which is what I want to do, so the flow of the application is kept unchanged.

rafn
  • 69
  • 2
  • 7
  • What is your intent? Do you want the ajax call to be redirected to a new URL which returns the actual response for the ajax call, OR, do you want the current whole page in the browser to be redirected to another url (Refreshed) ? – Zasz Aug 24 '11 at 11:08
  • I have tried to clarify above. – rafn Aug 24 '11 at 12:07

1 Answers1

0

Since $.ajax() does not create a <form> tag and submit it, you cannot do things like RedirectToAction, during ajax posts. ko.utils.postJson allowed you to do all that because it internally created a quick form tag in the dom and submitted it via script.

What you can do is :

$.ajax({
  statusCode: {
    302: function() {
      window.location.replace("http://Domain.com/Controller/Edit")
    }
  }
});

This question helped me find the way to redirect browser via script. This page tells you how to handle different status codes returned by the server. ASP.NET MVC3 returns 302 status code when you use RedirectToAction() in the Controller. And this can be handled, by adding the approperiate handler as shown above.

Community
  • 1
  • 1
Zasz
  • 12,330
  • 9
  • 43
  • 63
  • Ok this works quite good, I am just not to happy about keeping track on this in JavaScript. Perhaps I will revert to knockout, and implement some validation of the model which was my original problem... hmmm – rafn Aug 24 '11 at 13:52
  • If you are uncomfortable about putting the URL in the javascript, you can do as suggested in [this](http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) answer. Or even more suitable, look at [this](http://stackoverflow.com/questions/1557602/jquery-and-ajax-response-header) answer to actually read the URL to go to from the **Location** header from the response. – Zasz Aug 24 '11 at 18:24