2

hi guys i'm posting some data to controller using jquery ajax, but i am getting null values in my controller,

jQuery code is:

$('#registerCompOff').click(function() {

    var compOff = [];
    $('div').each(function() {
        var curRow = {};
        curRow.Description = $(this).find('.reason').val();
        curRow.CompOffDate = $(this).find('.datefieldWithWeekends').val();
        if (curRow.Description != null && curRow.CompOffDate != null) {
            compOff.push(curRow);
        }
    });
    $.ajax({
        type: 'POST',
        url: this.href,
        dataType: 'json',
        data: compOff

    });

    return $('form').valid();

});​

compOff is not null I have checked that...

controller is:

 [HttpPost]
        public ActionResult RegisterCompOff(RegisterCompOff[] registerCompOff)
        {

            //return View();
        }

can you tell me where i'm going wrong?

6
  • //later edit: he is right, create a key/value list and in the controller place the parameters as the keys Commented Jun 22, 2012 at 8:25
  • i'm sending an array..same i specified in controller... Commented Jun 22, 2012 at 8:27
  • 1
    Given your original code, change in $.ajax -> data: JSON.stringify(compOff) then add contentType: "application/json; charset=utf-8" and finally change parameter name of action controller to public ActionResult RegisterCompOff(RegisterCompOff[] compOff). Model binding should kick off then. It did for me. Commented Jun 22, 2012 at 10:09
  • yep that i tried and it worked like a charm..thanks lucask Commented Jun 22, 2012 at 10:32
  • lucask post ur comment ans...let me mark it as answer Commented Jun 22, 2012 at 10:54

3 Answers 3

3

Given your original code, change in $.ajax -> data: JSON.stringify(compOff) then add contentType: "application/json; charset=utf-8" and finally change parameter name of controller's action to public ActionResult RegisterCompOff(RegisterCompOff[] compOff).
Model binding should kick off then. It did for me.

Sign up to request clarification or add additional context in comments.

Comments

1

Edited:

try this :

 $.ajax({
        type: 'POST',
        url: this.href,
        dataType: 'json',
        traditional: true,
        data: 
        {
             CompOffList: compOff 
        }
    });

and change your controller like this :

[HttpPost]
        public ActionResult RegisterCompOff(List<RegisterCompOff> CompOffList)
        {

            //return View();
        }

hope this helps

8 Comments

well adding this content type give me network error...its not necessary to specify content type..
i have forgot a very important key/value pair (traditional: true).check again
are you sure that was a good idea, cause now i'm not getting anything in controller, count is 0 now...why do we need traditional here...BTW
yes.remove the bind attribute as i did.you need this, for detail check this ienablemuch.com/2011/05/…
@PiyushSardana you are getting null because you must declare list of RegisterCompOff in you controller action.
|
0

Your r passing javascript object as data wherease jquery ajax method expects a key/value pair list. Try this

data:{Description:compOff.Description, CompOffDate:compOff.CompOffDate}

2 Comments

Sorry missed the point that compOff is an array and u r expecting array in the action
yea but then i'm wraping up those javascript objects in an array...i have multiple objects there

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.