将多个对象传递给我的控制器 [英] Passing multiple objects to my controller
问题描述
我正在像这样将一个对象传递给我的控制器:
I am passing an object to my controller like so:
var form = JSON.stringify({
"subRevisedRequest": $('#frmRevised').val(),
"subSubcontractor": $('#frmSubcontractor').val(),
"subDescription": $('#frmDesc').val(),
"subCostCode": $('#frmCostCode').val(),
"subAmt": $('#frmAmt').val(),
"subPaymentTerms": "terms",
"subRetainage": 10,
"subComments": $('#frmComment').val()
});
$.ajax({
url: '@Url.Action("CreateSubcontracts", "Routing")',
type: "POST",
datatype: "JSON",
contentType: "application/json; charset=utf-8",
data: form,
success: function(result) {
if (!result.success) {
$('#errormsg').empty();
$('#errormsg').append(result.message);
} else {
location.href = '@Url.Action("Index", "Home")';
}
},
error: function (result) {
alert("Failed");
}
});
我的控制器将其视为它正在寻找的对象:
my controller sees this as the object it is looking for:
public ActionResult CreateSubcontracts(RoutingSubcontracts s)
我的问题是我只想再传递一个字符串.我知道我可以制作特定于视图的模型,但是我想知道是否可以执行以下操作:
My problem is that I'd like to pass along just one more string. I know I can make a view specific model but I was wondering if I could do something like this for example:
public ActionResult CreateSubcontracts(RoutingSubcontracts s, string bu)
我没有运气就尝试了以下方法:
I have tried the the following with no luck:
data: JSON.stringify({ "s": form, "bu": "251" }),
,但是复杂对象只是作为null出现.有没有办法让我也可以通过对象和字符串?
but the complex object just comes through as null. Is there a way I can pass the object and a string through as well?
推荐答案
尝试将字符串项添加到您已经拥有的JSON中.不要对其进行字符串化,否则它将发送一个大字符串,您将不得不在服务器上再次对其进行解析.
Try adding the string item in the JSON you already have. Dont stringify it or it will just send a big string that youll have to parse again on the server.
var form = {
"subRevisedRequest": $('#frmRevised').val(),
"subSubcontractor": $('#frmSubcontractor').val(),
"subDescription": $('#frmDesc').val(),
"subCostCode": $('#frmCostCode').val(),
"subAmt": $('#frmAmt').val(),
"subPaymentTerms": "terms",
"subRetainage": 10,
"subComments": $('#frmComment').val(),
"bu": "251" // add it here
};
$.ajax({
url: '@Url.Action("CreateSubcontracts", "Routing")',
type: "POST",
datatype: "JSON",
data: form,
success: function(result) {
if (!result.success) {
$('#errormsg').empty();
$('#errormsg').append(result.message);
} else {
location.href = '@Url.Action("Index", "Home")';
}
},
error: function (result) {
alert("Failed");
}
});
这篇关于将多个对象传递给我的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!