我有一个表单,我想将其数据传递到我的 Controller 。这是我正在进行的 JQuery 调用 -
var data = $form.serializeArray();
var options = {
contentType: "application/json",
url: "/Form/Save",
data: JSON.stringify(data),
type: "post",
datatype: "html",
success: function (result, xhr, status) {
console.log(result);
},
error: function (xhr, status, error) {
// redirect to error page
}
};
$.ajax(options);
这就是我在 Controller 中接收此信息的方式 -
public ActionResult Save(string paramJson)
{
// save paramJson
return null;
}
但是我在“保存”操作中收到的只是 paramJson = null。我也在下面尝试过 -
数据:JSON.stringify({paramJson: data})
但是没有成功。这里应该做什么?
最佳答案
我从上面的答案中得到了一些提示,并能够制定一个非常适合我的解决方案。我现在收到格式为 formname: formvalue
的 json,而不是 name: formname, value: formvalue
格式。这是 -
var json = {};
// converting to formname:formvalue format
$.each($form.serializeArray(), function (i, field) {
json[field.name] = field.value || '';
});
// stringify the parameter
var data = { paramJson: JSON.stringify(json) };
var options = {
contentType: "application/json",
url: "/Form/Save",
data: JSON.stringify(data),
type: "post",
datatype: "html",
success: function (result, xhr, status) {
console.log(result);
},
error: function (xhr, status, error) {
// redirect to error page
}
};
$.ajax(options);
关于jquery - 如何将serializeArray作为json传递给MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27317068/