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.

I use this code to get checked date,but it's not get the int[] value. Where is wrong.

View Page:

        function displayCheckedPersons() {
        var $checkedRecords = new Array();
        var j;
        j = 0;
           var nodes = $("#PersonTree").jstree("get_checked",null,true); 
           $.each(nodes, function (i, element) {
               if ($(element).attr("ifPerson") == "Y") {
                   $checkedRecords[j] = $(element).attr("id");
                   j = j + 1;
               };
           });

       if ($checkedRecords.length < 1) {
            alert('Please Select Persons first.');
            return;
        }


        $('#result').load('<%= Url.Action("DisplayCheckedPersons", "Scheduling") %>',$checkedRecords);
    }
</script>

Action Code:

        public ActionResult DisplayCheckedPersons(int[] checkedRecords)
    {
        AttendMSDataContext db = new AttendMSDataContext();
        checkedRecords = checkedRecords ?? new int[] { };
        return PartialView("CheckedPersons", db.Persons.Where(o => checkedRecords.Contains(o.id)));
    }

Edit by dai: I used $ajax to post the array,like this:

 $.ajax({
            type: "POST",
            url: "<%= Url.Action("DisplayCheckedPersons", "Scheduling") %>",
            data: { checkedRecords: checkedPersons },
            dataType: "html",
            success: function (request) { $("#result").html(request); },
            traditional: true
        });
share|improve this question
 
I'm don't think its the cause but you dont need to be using $ infront of checkedRecords. –  Henry Garle Aug 17 '11 at 20:40
add comment

1 Answer

up vote 0 down vote accepted

The data needs to be used like this:

$('#result').load('<%= Url.Action("DisplayCheckedPersons", "Scheduling") %>', $checkedRecords);

Becomes:

$('#result').load('<%= Url.Action("DisplayCheckedPersons", "Scheduling") %>', { checkedRecords: $checkedRecords });

The syntax is:

{ expectedName: JavascriptVariable, expectedName2: JavascriptVariable2 }

Edit:

Here is an Ajax example showing how to load your content. The .load example from abouve should work im not sure whats going wrong for you but here you go:

$.ajax({ 
    type: "POST",
    url: "<%= Url.Action("DisplayCheckedPersons", "Scheduling") %>",
    data: { checkedRecords: checkedPersons },
    dataType: "json",
    success: function(data) {
            $("#YourDiv").html(data);
        }
    });
share|improve this answer
 
The JavascriptVariable is $checkedRecords ,if use :$('#result').load('<%= Url.Action("DisplayCheckedPersons", "Scheduling") %>',{checkedRecords: checkedRecords});Get a error :checkedRecords is not defined –  dai.frank Aug 17 '11 at 21:55
 
Rename $checkedRecords to checkedRecords –  Henry Garle Aug 17 '11 at 23:26
 
Thanks for you help! But,the array values not passed to the function while renamed $checkedRecords to checkedPersons, the parameter int[] still null.I think the main problem is how to pass array values from javascript to asp.net mvc function by $.load(). –  dai.frank Aug 19 '11 at 7:15
 
@Steve, why add the $ back in its not really convention that was the whole point of removing it. –  Henry Garle Aug 19 '11 at 12:26
 
@dai, Have you checked firebug to see what data is being posted, if any? –  Henry Garle Aug 19 '11 at 12:27
show 8 more comments

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.