0

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
        });
1
  • I'm don't think its the cause but you dont need to be using $ infront of checkedRecords. Commented Aug 17, 2011 at 20:40

1 Answer 1

0

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);
        }
    });
Sign up to request clarification or add additional context in comments.

13 Comments

The JavascriptVariable is $checkedRecords ,if use :$('#result').load('<%= Url.Action("DisplayCheckedPersons", "Scheduling") %>',{checkedRecords: checkedRecords});Get a error :checkedRecords is not defined
Rename $checkedRecords to checkedRecords
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().
@Steve, why add the $ back in its not really convention that was the whole point of removing it.
@dai, Have you checked firebug to see what data is being posted, if any?
|

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.