Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Knowing the values of check box in the controller with razor

I have a asp.net mvc application with razor engine. In a view Home i have this snippet:

<section  id="form_admin">
    <form action="/Super/Manipuler" method="post">
    <fieldset>
        <legend>Formulaire d'ajout d'un administrateur</legend>
           @Html.Label("Login")
           @Html.Label("Mail")
           @Html.Label("Password")
           @Html.Label("Name")
           <br />
           <br />    

    @if(Model != null){
    foreach (Upload.Models.AdminModels admin in Model)
    {
            if (i == 0){
                <input type="radio" checked class="radio" name="radio"  value="@admin.Login" >
            }
            else{
                <input type="radio"   class="radio" name="radio" value="@admin.Login" style="margin-left:0.3px;">
            }      
    <label id="log">@admin.Login</label>
    <label id="logm">@admin.Mail</label>
    <label id="logp">@admin.Password</label>
    <label id="logn">@admin.Name</label>
    <br />
            i++;
    }
    }

         <br />
         <input type="submit" value="Editer"  name="submit_button"/>
         <input type="submit" value="Supprimer" name="submit_button" />

     <a href="@Url.Action("Admin_Creation", "Super")" style="color:blue">Créer un nouveau compte</a>
    </fieldset>
  </form>
</section>

In the controller : the action Manipuler is the below:

  public ActionResult Manipuler()
        {
            string buttonName = Request.Form["submit_button"];
            string _login = Request.Params["radio"];

            Upload.Models.AdminModels admin = new AdminModels();
            Upload.Models.CompteModels.Modifiying_login = _login;

            if (buttonName == "Editer") { return RedirectToAction("Edit", "Admin"); }

            else { admin.Delete_admin(_login); return RedirectToAction("Home", "Super"); }
 }

It's works fine but i'd like to change the radiobox to checkbox. My question is how to know all checked box in the collection of checkbox in the action Manipuler ?

Upvotes: 0

Views: 1111

Answers (1)

Andy T
Andy T

Reputation: 9881

Take a look at Phil Haack's article on model binding a checkbox list. Basically, you just need to set up the HTML in a specific way (name your checkboxes the same which will then convert the various POSTed values into a list).

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Upvotes: 1

Related Questions