[英]Sending Complex Objects to Controller with Ajax
I have an MVC application where I am trying to submit the form through an ajax call while also passing in another complex object.我有一个 MVC 应用程序,我试图通过 ajax 调用提交表单,同时还传入另一个复杂的 object。 This means I am passing 2 complex objects back to the controller: the form data and an object array.
这意味着我将 2 个复杂对象传递回 controller:表单数据和 object 数组。 The problem is the form data makes it to the controller fine if I set the contentType to "application/x-www-form-urlencoded" but the object array comes over as null.
问题是,如果我将 contentType 设置为“application/x-www-form-urlencoded”,但 object 数组作为 Z37A6259CC0C1DAE299A7866489DFF0BD. If I set the contentType to "application/json", the object array makes it to the controller just fine, but the form data is null.
如果我将 contentType 设置为“application/json”,则 object 数组将其变为 controller 就好了,但表单数据是 null。 So I think I basically need to convert the form data to a json object.
所以我想我基本上需要将表单数据转换为json object。 Here's what I've got:
这是我所拥有的:
ViewModel:视图模型:
Public FOO foo
{
public ObjectBar BarObject {get; set;}
public string BarString {get; set;}
// more properties
}
Controller: Controller:
[HTTPPost]
Public ActionResult Submit(Foo viewModel, OtherObject[] dataSet)
{
//do stuff
}
View:看法:
// bunch of fields
<script>
function submitForm(e) {
debugger;
var dataSets = getGridData();
var form = $("#formName").serialize();
var data = JSON.stringify({ viewModel: form, dataSet: dataSets }); //this is close, need to convert form data to json
return $.ajax({
type: "POST",
url: '@Url.Action("Submit", "Report")',
data: data,
contentType: 'application/json',
processData: false,
});
}
//some more javascript stuff
</script>
Searching around stack overflow, it seems like all I need to do to convert the form data to json is something like搜索堆栈溢出,似乎我需要做的就是将表单数据转换为 json 类似于
var jsonForm = JSON.parse(JSON.stringify(form));
but for some reason that is not working, even if I just try to pass just jsonForm to the controller witrh just the viewModel parameter.但由于某种原因,这不起作用,即使我只是尝试将 jsonForm 传递给 controller 与 viewModel 参数。 Should I be using something else to convert, or am I just missing something?
我应该使用其他东西来转换,还是我只是错过了一些东西?
With some help from this answer in stack overflow:在堆栈溢出的这个答案的帮助下:
Convert form data to JavaScript object with jQuery 使用 jQuery 将表单数据转换为 JavaScript object
I found this statement:我发现了这个说法:
$('form').serializeArray().map(function(x){this[x.name] = x.value; return this;}.bind({}))[0] $('form').serializeArray().map(function(x){this[x.name] = x.value; return this;}.bind({}))[0]
So I updated my code and wound up with this:所以我更新了我的代码并结束了这个:
function submitForm(e) {
debugger;
var dataSets = getGridData();
var form = $("#formName").serializeArray().map(function (x) { this[x.name] = x.value; return this; }.bind({}))[0];
var data = JSON.stringify({ viewModel: form, dataSets: dataSets }); //this is close, need to convert form to json... now this works!
return $.ajax({
type: "POST",
url: '@Url.Action("Submit", "Report")',
data: data,
contentType: 'application/json; charset=utf-8',
processData: true,
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.