1

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" }),

but the complex object just comes through as null. Is there a way I can pass the object and a string through as well?

Herrozerro
  • 1,601
  • 1
  • 22
  • 37

2 Answers2

1

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");
    }
});
Brad C
  • 2,868
  • 22
  • 33
  • for some reason i still need to stringlfy the first json object, otherwise I am just getting 500 errors. but adding it seems to have fixed it. thanks! – Herrozerro Jul 01 '15 at 12:42
0

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

Gregg
  • 615
  • 6
  • 6