jeudi 29 octobre 2015

Getting checked checkboxes' name, access another view ect

I am extremely new to ASP.NET MVC/JavaScript and frankly I don't, yet, understand the concept but this is my problem. I am trying to make an online pizza order system. The current problem I am facing is checking which checkboxes is selected(the ingredients that the person wants on their pizza) and passing that to another view, which will present the user with a break-down of what they ordered and with prices ext. What I have is a couple of checkboxes:

<div style="display: inline; font-family:Arial; font-size: 16px; color: black; background: rgba(255,255,255,0.5)"> <br />
        <input type="checkbox" name="Cheese" value="Cheese" />Cheese(R2.00) <br />
        <input type="checkbox" name="Capers" value="Capers" />Capers(R3.00) <br />
        <input type="checkbox" name="Banana" value="Banana" />Banana(R2.00) <br />
        <input type="checkbox" name="Avocado" value="Avocado" />Avocado(R4.00) <br />
        <input type="checkbox" name="Chicken" value="Chicken" />Chicken(R5.00) <br />
        <input type="checkbox" name="Anchovies" value="Anchovies" />Anchovies(R5.00) <br />
        <input type="checkbox" name="Sausage" value="Sausage" />Sausage(R5.00) <br />
        <input type="checkbox" name="Mince" value="Mince" />Mince(R6.00) <br />
        <input type="checkbox" name="Pepperoni" value="Pepperoni" />Pepperoni(R5.00) <br />
        <input type="checkbox" name="Spinach" value="Spinach" />Spinach(R4.00) <br />
        <input type="checkbox" name="Fresh Chilis" value="Fresh Chilis" />Fresh Chilis(R8.00) <br />
        <input type="checkbox" name="Green Peppers" value="Green Peppers" />Green Peppers(5.00) <br />
        <input type="checkbox" name="Bacon" value="Bacon" />Bacon(R6.00)<br />
        <input type="checkbox" name="Pineapple" value="Pineapple" />Pineapple(R3.00) <br />
        <input type="checkbox" name="Ham" value="Ham" />Ham(R2.00) <br />
        <input type="checkbox" name="Olives" value="Olives" />Olives(R9.00) <br />
        <input type="checkbox" name="Onions" value="Onions" />Onions(R3.00)
    </div> 

I came up with this piece of code, for the above mentioned problem.

@using (Html.BeginForm("Final", "Home"))
    {
            //I also tried saving to a list but didn't know how to access it from another view
            List<string> ingredientsChecked = new List<string>();
            <script>
            $('#checkBox input:checkbox').addClass('input_hidden');
            $('#checkBox checkbox').click(function () {
                var elementC = document.getElementsByName(this.name);
                for (var x = 0; x < elementC.length; x++)
                {
                    if (elementC[x].checked == true)
                    {
                        elementC[x].value;
                    }
                }
                var connection = new ActiveXObject("ADODB.Connection");
                var connectionstring = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\xxxxxxxxxx\ThePizzatorium\ThePizzatorium\App_Data\ThePizzatoriumDataBase.mdf;Integrated Security=True;Connect Timeout=30";
                connection.Open(connectionstring);
                var rs = new ActiveXObject("ADODB.Recordset");
                rs.Open("SELECT * FROM table", connection);
                rs.MoveFirst
                while(!rs.eof)
                {
                    document.write(rs.fields(1));
                    rs.movenext;
                }
                rs.close;
                connection.close;
            });
        </script>
        <input type="submit" value="Order Pizza" class="btn-primary" />
    }

But it doesn't work. Is there a better way than checking which checkbox is checked; writing the value to a sql database, thus reading the checked checkboxes from the other view via the sql database? Maybe saving it in a Session? If so, how would I do that? Any and all help will be appreciated!




Aucun commentaire:

Enregistrer un commentaire