3

I have a jQuery function that is called when the "submit" button is clicked:

function SubmitForm() {
    var idList = [];
    var list = $('#TableAdminPortfolio .CheckBoxProjects');
    list.each(function () {
        var id = $(this).closest('td').children('.hiddenId').val(); // parseInt()
        idList.push(id);
    });

    $.ajax({
        url: $(this).href,
        type: 'POST',
        data: idList,
        success: function (result) {alert('Successful');},
        error: function (result)  {alert('Error');}

    });
}

My controller looks like:

[Transaction]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(IEnumerable<int> projects)
{
    ...
}

The paramater (projects) is always null. I've stepped through my jQuery code inspecting it each step of the way and the idList is definitely populated. I've also tried my $ajax like this:

$.ajax({
        url: $(this).href,
        type: 'POST',
        data: { projects : idList },
        success: function (result) {alert('Successful');},
        error: function (result)  {alert('Error');}

    });

And still the same results. Any ideas what is going on? And yes, I have a reason for doing an Ajax Post rather then a Form Post.

TIA

NOTE: I am using jQuery v1.6.4 and ASP.NET MVC 2.0.

8
  • If you are debugging, does the ajax post hit your Index method? Commented Feb 12, 2013 at 23:09
  • $(this).href === undefined Commented Feb 12, 2013 at 23:14
  • You are posting an array of integers to ASP.NET MVC. How is it supposed to convert an array of integers into an array of projects? Are you assuming it do a lookup for each integer and find the project that matches the integer id? There is some "magic" to how ASP.NET MVC binds objects. You should look closely at some code samples to understand this. The book "ASP.NET MVC in Action" is one place to start. Commented Feb 12, 2013 at 23:16
  • It does hit the index method. That is how I know it is null. Commented Feb 12, 2013 at 23:31
  • The parameter is an IEnumerable<int>. Once I have the list of Ints, I check it against a very simple data store to see if I need to add or delete members from the database. Commented Feb 12, 2013 at 23:33

2 Answers 2

2

try converting your array to json using JSON.stringify

$.ajax({
    url: $(this).href,
    type: 'POST',
    dataType: "json",
    data: JSON.stringify(idList),
    traditional: true,
    success: function (result) {alert('Successful');},
    error: function (result)  {alert('Error');}

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

3 Comments

I am getting the same results. Before entering the AJAX call my idList is populated. I then continue and hit my breakpoint in my controller where the parameter is NULL. <grrr/>
@KeithBarrows, looking through similar code I have, they all include traditional: true. try that.
@KeithBarrows, try traditional:true with and without stringify
0

try:

 var mylist='{"projects":'+ JSON.stringify(idList)+'}';

then

data:mylist,

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.