mardi 4 août 2015

Is there a way to acess the values of alot of form collection elements without manually typing the reference value associated with each element?

The above question is for ASP.NET MVC. I had to delete that part because of the title size restriction.

Let's say I have 100 checkboxes. Each checkbox has a reference value integer from 1 to 100. Some checkboxes are checked. Some are not. I press submit. A form collection is passed into an action method. Each checkbox of the form collection either has a value that represents if it is checked or not. Now I want to be able to access the values of each checkbox without manually typing the reference values from 1 to 100.

I've already tried just looping from 1- 100 over a FormCollection. In other words, I have the strings "1" to "100" created (not manually) to be put inside let's say a string a. Then I want form collection name[a] to be processed. Unfor. that does not happen.

Can somebody tell me why this happens?

Can somebody give me a simple alternative?

Here is my code. Below, on the Form_Data[b], my debugger gives a null reference exception. If I put b="2(or any other integer from 3 to 91 inclusive)", my code works fine(which is why I suspect form collection reference values can only be read during compile-time).

for (int Speaker_Reference_Number = 2; Speaker_Reference_Number <= 91; Speaker_Reference_Number++)
{
    char a = '"';
    string c = a.ToString(); 
    string b ="";
    b = b + c;
    b = b + Speaker_Reference_Number;
    b = b + c;

    if (Form_Data[b].Equals("true,false"))
    {
        if (Speaker_Reference_Number <= 9)
        {
            Speakers = Speakers + "00" + Speaker_Reference_Number;
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire