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?

2 Answers 2

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");
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

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!
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.