我正在将一个对象传递给我的控制器,如下所示:
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"); } });
我的控制器将其视为它正在寻找的对象:
public ActionResult CreateSubcontracts(RoutingSubcontracts s)
我的问题是我只想再传递一个字符串。我知道我可以制作特定于视图的模型,但我想知道是否可以执行以下操作:
public ActionResult CreateSubcontracts(RoutingSubcontracts s, string bu)
我没有运气就尝试了以下方法:
data: JSON.stringify({ "s": form, "bu": "251" }),
但是复杂对象只是作为null来的。有没有办法我也可以传递对象和字符串?
尝试将字符串项添加到已有的JSON中。不要对其进行字符串化,否则它将发送一个大字符串,您必须在服务器上再次对其进行解析。
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"); } });