0

I have JQuery code for creating storing a collection of checked checkboxes boxes as an array. This array is stored as the value of a hidden field on my form with the ID of 'CheckedSubGroups. The hidden field has been given the name of a List from my controller:

Model:

public IList<int> SubtGroupPkids { get; set; }

Hidden Field on Form:

<input type="hidden" id="CheckedSubGroups" name="SubGroupPkids" value="[]" />

JQuery for adding items to the array.

$(".SubGroupCheckBoxes").on("click", function() {
            if ($(this).is(':checked')) {
                var subGroupArray = JSON.parse($("#CheckedSubGroups").val());
                subGroupArray.push($(this).attr('data-subGroupPkid'));
                $("#CheckedSubGroups").val(JSON.stringify(subGroupArray));

if I add a couple of items this array looks like (from the debugger):

[15330,16657]
[prototype]: []
length: 2
[0]: "15330"
[1]: "16657"

I then serialize the form using JQuery's .serialize method.

However I get a binding error where it fails to convert the array to the List.

The value '["15330","16657"]' is not valid for SubGroupPkids.

Where am I going wrong?

1
  • I assume it's actually IList<int> SubGroupPkids and not IList<int> SubtGroupPkids Commented May 11, 2015 at 22:46

1 Answer 1

1

You will need to de-serialize the json array in the Controller

 var deserializer = new JavaScriptSerializer();
 subGroupPkIds = deserializer.Deserialize<List<int>>(serializedData "data submitted to your controller");
Sign up to request clarification or add additional context in comments.

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.