1

It's verys simple situation in fact, but I can't find solution: I need pass JSON string to my MVC Controller via jQuery ajax but Controller always receive null. Here is the jQuery:

$("#makebooking").click(function (e) {
            var json;
            if ($("form").valid()) {
                var arr = $("form").serializeArray();
                json = JSON.stringify({ 'command': arr });
            }
            $.ajax({
                url: "@Url.Content("~/Booking/CreateBooking")",
                data: json,
                type: "post",
                cache: false,
                dataType: "json",
                success: function (result) {
                    if (result != null) {
                        window.location = result;
                    } else {
                        $("#modalerror").on("show.bs.modal", function () {
                            var modal = $(this);
                            modal.find("#errormsg").text(result.Error);
                        });
                    }
                },
                error: function (xhr) {
                    var err = eval("(" + xhr.responseText + ")");
                    alert(err.Message);
                }
            });

And Controller code is here:

        [HttpPost]
        public JsonResult CreateBooking(string command)
        {           
            byte[] stream = HttpServerUtility.UrlTokenDecode(Request.Cookies["psw"].Value);
            byte[] decodedValue = MachineKey.Unprotect(stream, "all");
            var psw = Encoding.UTF8.GetString(decodedValue);
            var a = _br.CreateBooking(User.Identity.Name, psw, command);
            return Json(a);
        }

I hope someone can have fresh look at this code and advise solution. Thanks.

8
  • Why are you posting a string (as opposed to posting your model)? Commented Feb 28, 2015 at 11:15
  • @StephenMuecke, I'm not sure what you mean. Can you explain please? Commented Feb 28, 2015 at 11:16
  • The parameter in you method is string command? You have a form which I assume is based on a model, in which case you should post back you model using data: $('form').serialize(), and the method should be` public JsonResult CreateBooking(YourModel model)` Commented Feb 28, 2015 at 11:21
  • Yes, form based on ViewModel. Just now I tired to use model as parameter but without success - all data are null. Commented Feb 28, 2015 at 11:28
  • 1
    One more reason why I'm using string - I need process this data in order to re-create its as urlencoded string for calling external web service. Commented Feb 28, 2015 at 11:29

2 Answers 2

1

The parameter of you POST method is string but you posting back json.

Change the method to

[HttpPost]
public JsonResult CreateBooking(YourModel model)

and the script to

if ($("form").valid()) {
  var json = $('form').serialize(); // serialize the form values to json
  $.ajax({
    url: '@Url.Action("CreateBooking", "CreateBooking")', // recommended
    data: json,
    ....

You model will be correctly bound in the POST method

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure if this is the entirety of your problem, but it looks like the url in your ajax call has a problem with the quotes. Try mixing single quotes with double quotes such as

url: "@Url.Content('~/Booking/CreateBooking')"

6 Comments

Definitely it's not the source of this issue.
Have you tried using the inspector in chrome or firefox to check the call being made to the server?
Yes. I see that json data passed to ajax call, but missed somewhere between jQuery and Controller.
@hobenkr, This makes no difference (although its normal to use url: '@Url.Action("CreateBooking", "Booking")',
@stephenmuecke, you're right. Thanks for the correction. Andrey, I'm not familiar with asp, but do you get a response from the server in inspector? Maybe an error message or something?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.