mercredi 4 décembre 2019

How do I pass CheckBox values from a view to a controller using @Html.CheckBox?

I need to display 6 types of tests on my view and allow the user to check as many tests as they'd like to complete. I will then need to send their response to my controller so I can add that information to a database using the entity framework.

my View:

@using (Html.BeginForm("Quote", "Customers", FormMethod.Post))
{
    <label for="test1"> BP</label>
    @Html.CheckBox("test1", true)<br />

    <label for="test2"> DS</label>
    @Html.CheckBox("test2")<br />

    <label for="test3"> IS</label>
    @Html.CheckBox("test3")<br />

    <label for="test4" class="iput">PS</label>
    @Html.CheckBox("test4")<br />

    <label for="test5">PF</label>
    @Html.CheckBox("test5")<br />

    <label for="test6">CS</label>
    @Html.CheckBox("test6")<br />

    <input type="submit" value="Begin Quote" class="btn btn-default" style="padding-left: 20px" />
} 

and my controller method:

[HttpPost]
        public ActionResult Quote(FormCollection collection )
        {

            if (!string.IsNullOrEmpty(collection["test1"]))
               {
                   string checkResp = collection["test1"];
                   bool checkRespB = Convert.ToBoolean(checkResp);
               }


            return View("Index");
        }

I am running into an issue when debugging...The collection["test1"] is returning a "true, false" value instead of the actual checked value of the checkbox. I cannot seem to get the form to collect the actual statuses of the check boxes.

I have tried other options but none have seemed to work. Any help is greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire