Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it posible with ajax to send one json-array (array with json objects) and also include a separate parameter to receive in MVC action method?

JavaScript:

var n = {
    number: 1           
};

$.ajax({
    type: "POST",
    url: url,
    contentType: "application/json; charset=utf-8",
    dataType: "html",
    data: JSON.stringify({jsonObjects:json, number:n}),
    success: function (response) {
        $('#body').html(response)
    }

});

MVC action:

public ActionResult Create(List<JsonObjects> jsonObjects, int? number)

JsonObjects is coming in like it supposed to but number is null.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

It seems like your n variable declaration is wrong.

Try the following

var n = 1;

$.ajax({
        type: "POST",
        url: url,
        contentType: "application/json; charset=utf-8",
        dataType: "html",
        data: JSON.stringify({jsonObjects:json, number:n}),
        success: function (response) {
            $('#body').html(response)
        }

        }
    });
share|improve this answer
add comment

Try the following code:

var n = 1;

$.ajax({
    type: "POST",
    url: url,
    contentType: "application/json; charset=utf-8",
    dataType: "html",
    data: json+ "&number=" + n,
    success: function (response) {
        $('#body').html(response)
    }

});

I apologise if this doesn't work as I am unable to test this situation exactly, but the following line should be roughly what you are after and has worked for me in similar situations:

data: json+ "&number=" + n,

Also, see this related question which may help you fix your problem.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.