CheckboxList in ASP.NET MVC

7

I need to create a checkboxlist on my page using MVC (Razor). I noticed that there is no Html helper of type Html.CheckboxlistFor or something like that.

Someone has some example of how I could do this. If possible, what would the code look like in the controller in the actions for popular and then to capture the selected checkboxes?

    
asked by anonymous 17.02.2014 / 21:11

3 answers

8

There is a package called MvcCheckBoxList (whose helper is called CheckBoxListFor ):

link

Code examples are in the link (but it is good to reproduce something):

View in Razor

@using MvcCheckBoxList.Model
@model CheckBoxListForApp.Models.FruitViewModel

@{
    ViewBag.Title = "Home Page";
}

<section class="checkBoxListFor">
    <p>
        <label>Please Select Fruits:</label>
        @using (Html.BeginForm("Index", "Home", FormMethod.Post))
        {
            @Html.CheckBoxListFor(model => model.PostedFruits.FruitIds,
                                  model => model.AvailableFruits,
                                  fruit => fruit.Id,
                                  fruit => fruit.Name,
                                  model => model.SelectedFruits,
                                  Position.Horizontal)
            <input class="green" type="submit" 
                      value="POST to Controller" />
        }
    </p>
</section>

Arguments:

  • First: Which Id's will be sent to the Controller at POST;
  • Second: Available options;
  • Third: Which field of the data object is considered Id;
  • Fourth: Which field of the data object is considered a description of the Checkbox;
  • Fifth: A list of options that have already been marked (mount on the Controller);
  • Sixth: The orientation of CheckBoxList (horizontal or vertical).

NuGet: link

    
17.02.2014 / 22:56
4

@MatheusBessa's answer works for a checkbox. For a list, you need to use the mapping structure for an array. See what changes:

A view:

@using (Html.BeginForm("Index", "Home"))
{
    <input type="checkbox" name="CheckboxList[0]" />
    <input type="checkbox" name="CheckboxList[1]" />
    <input type="checkbox" name="CheckboxList[2]" />
    ...
    <input type="checkbox" name="CheckboxList[n]" />
    <input type="submit" value="Submit"/>
}

And the controller:

[HttpPost]
public ActionResult Index(String[] CheckboxList)
{
    ...
}

It is natural that your viewmodel has more information. In case this would be a property of the viewmodel.

It's important to note that indexes need to be sequential and start at 0. If this is not respected, the model binder will not accept as an enumerator.

As @MatheusBessa commented, a checked checkbox will give the string on. Anything other than that represents the clean checkbox.

    
17.02.2014 / 21:35
0

Try this.

View:

    @using (Html.BeginForm("Index", "Home"))
    {
        <input type="checkbox" name="exemplo" />
        <input type="submit" value="Submit"/>
    }

Controller:

    [HttpPost]
    public ActionResult Index(String exemplo)
    {
        return View();
    }

If it is checked it will return on , otherwise it will return null .

    
17.02.2014 / 21:23