1

Why are the unchecked checkboxes not in the collection? I can only access the values from the checked ones.

I need it because i want to delete all unchecked from my DB.

Here a code sample:

foreach (var item in collection.AllKeys.Where(c => c.StartsWith("check_projekt_")).Select((x, i) => new { Data = x, Index = i }))
{
                if (collection[item.Data] == "1")
                {
                    dbclass.addUserToProjekt(Convert.ToInt32(collection["projekt_" + item.Index]), Convert.ToDouble(collection["input_projekt_" + item.Index]), mID);
                }
                else {
                    dbclass.deleteUserFromProjekt(Convert.ToInt32(collection["projekt_" + item.Index]), mID);
                }
}

TIA

lifeofbenschi
  • 777
  • 1
  • 6
  • 12

1 Answers1

0

You can use the @Html.CheckBox("cbName",true) method in Razor to create the checkbox. MVC also adds a hidden input field for each checkbox, so you can see everything with a true/false state. See this post for more info:

Razor ViewEngine HTML.Checkbox method creates a hidden input. Why?

Community
  • 1
  • 1
Dan A.
  • 2,914
  • 18
  • 23
  • Ok i tried it, but when i try to get the value from the collection, then i'm getting "true,false" as value ?? – lifeofbenschi Jan 26 '12 at 21:26
  • Hmmm. You may have to bind your collection to a view model for that approach to work. Ideally, you should be using view models anyway, rather than working with the FormCollection directly. – Dan A. Jan 26 '12 at 21:40
  • Yes, i know - but it worked with FormCollection now. (I checked it with "true,false" string hehe :) – lifeofbenschi Jan 27 '12 at 08:15