3

i am writing a function in jquery which post the data to controller. currently it is posting form data to controller fine but when i post checkbox list with form data then it send always count 0 in controller here is my code.

    function SubmitForm() {
    var studentFormData = $("#frmStudent").serialize();
    debugger;
    var SubjectArraydata = new Array();

    $(".chkSubject:checked").each(function () {
        var row = {
            "SubjectId": $(this).data("id")
        };
        SubjectArraydata.push(row);
    });

    $.ajax({
        url: '@Url.Action("StudentForm", "Student")',
        type: "POST",
        dataType: "json",
        data: studentFormData + JSON.stringify("&subjectData=" + SubjectArraydata),
        async: true,

        success: function (msg) {

        },
        error: function () {

        }
    });
}

Controller:

[HttpPost] 
public ActionResult StudentForm(Student student, List<Subject> subjectData)
{ 
   return Json(true); 
} 

any one tell me where is the problem in my code thank you.

teo van kot
  • 12,350
  • 10
  • 38
  • 70
Ammar
  • 686
  • 11
  • 28

2 Answers2

1

Your cannot mix 'application/x-www-form-urlencoded' data (the contentType of your serialize() method) and 'application/json' data (the contentType of the JSON.stringify() method) like that.

Sinve you have confirmed that your only submitting one property of Subject, which is SubjectId and is typeof int, then you can append the SubjectId values to the serialized data.

var studentFormData = $("#frmStudent").serialize();
$(".chkSubject:checked").each(function () {
    studentFormData += '&' + $.param({ SubjectIds: $(this).data("id") });
};
$.ajax({
    url: '@Url.Action("StudentForm", "Student")',
    type: "POST",
    dataType: "json",
    data: studentFormData,
    success: function (msg) {
    },
    error: function () {
    }
});

and change your controller method to

[HttpPost] 
public ActionResult StudentForm(Student student, List<int> SubjectIds)
{ 
    ....
  • thank you @Stephen its working fine.thank you for your guaidence. – Ammar Jun 16 '17 at 13:41
  • Just as a side note, its would be more 'MVC' to actually use a view model and generate the form controls based on that view model so that all that's needed is `$("#frmStudent").serialize()` - for an example of generating a collection of checkboxes, refer [this answer](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) –  Jun 16 '17 at 13:45
0

I think you use 'POST' method not correctly. You try to mix sending data as json and as url parameters.

data: studentFormData + JSON.stringify("&subjectData=" + SubjectArraydata),

what you send in data:

[
  {...},
  [{SubjectId: ''}, {SubjectId: ''}]
]

or:

{
  1: {...},
  subjectData: [{SubjectId: ''}, {SubjectId: ''}]
}

or some data sended as json, some in url?

Send all data in json, and dont serialize (jquery do it for you):

var data = [strudentFormData, subjectData];
$.ajax(..., data: data, ...);
Alexey Obukhov
  • 834
  • 9
  • 18