Checkboxlist on ASP.NET MVC

Asked

Viewed 6,710 times

7

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

Someone has some example of how I could do that. If possible show how the code in the controller would look in the shares to popular and then to capture the selected checkbox?

3 answers

8


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

http://www.codeproject.com/Tips/613785/How-to-Use-CheckBoxListFor-With-ASP-NET-MVC-4

Examples of codes are on the link (but it’s good to play 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 in the POST;
  • Second: The options available;
  • Third: Which data object field is considered Id;
  • Fourth: Which field of the data object is considered to be a description of the Checkbox;
  • Fifth: A list of options that have already been checked (mount to Controller);
  • Sixth: The orientation of CheckBoxList (horizontal or vertical).

Nuget: http://www.nuget.org/packages/MvcCheckBoxList/

4

The @Matheusbessa response works for a checkbox. For a list, you need to use the mapping structure for an array. See what changes:

The 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. If necessary, this array would be a viewmodel Property.

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

As @Matheusbessa commented, a checkbox marked will give the string on. Anything other than this represents the clean checkbox.

  • If you want another value besides "on", you can set the attribute value from checkbox to desired, such as a specific ID or text.

0

Try to use 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’s checked he’ll return on, else he will return null.

  • 1

    With a checkbox, da para utilizar o Html.Checkboxfor, e eh tranquilo, the problem eh when I have for example a list of categories and I want the user to choose one or more options, then I will have multiple checkboxes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.