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.
string command? You have a form which I assume is based on a model, in which case you should post back you model usingdata: $('form').serialize(),and the method should be` public JsonResult CreateBooking(YourModel model)`