Passing multiple objects to my controller

后端 未结 2 1179
南笙
南笙 2021-01-24 04:32

I am passing an object to my controller like so:

var form = JSON.stringify({
        \"subRevisedRequest\": $(\'#frmRevised\').val(),
        \"subSubcontractor\         


        
相关标签:
2条回答
  • 2021-01-24 04:44

    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");
        }
    });
    
    0 讨论(0)
  • 2021-01-24 04:56

    In your view jquery create a second var for bu. Assign the data in you ajax call like this;

    data: { "s" : form, "bu" : "251" }
    

    In your controller method change the signature to include a default value for bu like this;

    public ActionResult CreateSubcontracts(RoutingSubcontracts s, string bu = "NoValue")
    

    With the default value set bu will act like an optional parameter

    0 讨论(0)
提交回复
热议问题