0

I've been wrestling with this for hours and I can't seem to apply any kind of logic that will work.I have a collection of objects, lets call them items, that have a boolean property to define if they are to be included later when adding to a basket. The problem I'm having is that I want a page that lists these items and has a checkbox next to each one, which toggles whether the item is to be included or not.

I have this:

@using(Ajax.BeginForm("UpdateItems", "Controller", null, 
       new AjaxOptions { HttpMethod = "Post"}, new { id = "UpdateItemsForm" })
{
    @foreach(var item in collection)
    {
        @Html.CheckBox("itemIncluded", item.Included)
    }
}

<script>

$("#UpdateItemsForm").change(function ()
{
    $(this).submit();
});

</script>

In Controller I have

public JsonResult UpdateItems(bool[] itemIncluded)
{
   //Do Something unimportant
}

The problem is that in my controller I am getting an array of bools, which I want, but the length of the array is inconsistent, when I change a checkbox I will receive an array of n elements, depending on how many boxes are checked, e.g if I check 2/4 boxes I'll get an array of 6 bools. This array will contain a true and a false element for the box checked. Is there any way I could enforce an array size, i.e. Always get an array with a single element for each checkbox, with true or false if it is checked or not?

7
  • 2
    Why don't you use @Html.CheckBoxFor? If you'd post that you would get whether it's selected as well. Commented Oct 24, 2013 at 12:37
  • Can you post the html output of your view? Commented Oct 24, 2013 at 12:37
  • 1
    @bump, or even more concise, @Html.DisplayFor(collection) and then just build a custom display. Commented Oct 24, 2013 at 12:39
  • @bump when I use @Html.CheckBoxFor I get null, not an array, in the controller. Commented Oct 24, 2013 at 13:25
  • stackoverflow.com/questions/9973977/… Commented Oct 24, 2013 at 13:48

2 Answers 2

0

I changed my controller to use a FormCollection rather than an array. It then became a simple case of parsing the text from the checkbox in the controller, which is the very silly "true,false" for a true condition, or "false" for false. This way, you will always get the array the correct length, and be able to handle both true and false conditions for the checkboxes. Also, a small note, in order for this to work the checkboxes should have unique names with some common string in them, in my case I simply did this:

{
   var counter = 0;
}

@foreach(var item in Model.collection)
{
   <div class="item">
    @Html.Checkbox("item"+counter.ToString(), item.Included)
   </div>
    count++;
}

and then in the controller

int counter = 0;
foreach(var item in sessionCollection)
{
    string index = string.Concat("item", counter.ToString());
    string valueFromCheck = formCollection[index];
    if(valueFromCheck == "true,false")
        item.Included = true;
    if(valueFromCheck == "false")
        item.Included = false;
    i++;
}

Not the most elegant way of doing things, I know, but it works, and that's the important thing, premature optimization and trying to be a code smart arse always leads to problems.

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

Comments

-1

It's not a bug, it's a feature ;)

http://forums.asp.net/t/1314753.aspx

1 Comment

You should not post links without any description. See "link rot" questions on meta.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.