0

I have the following checkbox list:

<input type="checkbox" name="Categories" value="1" />
<input type="checkbox" name="Categories" value="2" />

My model is as follows:

public class MyModel
{
  public string Categories { get; set; }

}

My controller:

 public ActionResult Index(MyModel model)
 {
    if (ModelState.IsValid)
    {

        // Save data to database, and redirect to Success page.

        return RedirectToAction("Success");
    }

 }

Selecting both checkboxes saves only one value?

2 Answers 2

1

You can't get comma separated value directly to server, I suggest to change class as below

public class MyModel
{
   public List<string> Categories { get; set; }

}

You get list of values which checkbox selected.

If you want comma separated value then on client side need when submit form create function and need to save on hidden variable.

May this help you

Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

or do string.join(",", model.Categories)
0

The main problem is string property stores single string, you should use collection property to bind with checkboxes e.g. List<string> as @Ajay mentioned before.

Therefore, you should use this setup:

Model

public class MyModel
{
    public MyModel()
    {
        SelectedCategories = new List<string>();

        // put categories here
        Categories = new List<SelectListItem>() { ... };
    }

    // other properties

    public List<string> SelectedCategories { get; set; }
    public List<SelectListItem> Categories { get; set; }
}

View

@foreach (var item in Model.Categories)
{
    <input type="checkbox" name="SelectedCategories" value="@item.Value" ... />
}

Controller

[HttpPost]
public ActionResult Index(MyModel model)
{
    if (ModelState.IsValid)
    {
        // create comma-separated values
        var selectedCategories = string.Join(",", model.SelectedCategories);

        // Save data to database, and redirect to Success page.

        return RedirectToAction("Success");
    }
}

If you want to use CheckBoxFor helper, use SelectListItem which has Selected property with bool type, because CheckBoxFor binds for bool property:

Model

public class MyModel
{
    public MyModel()
    {
        // put categories here
        Categories = new List<SelectListItem>() { ... };
    }

    // other properties
    public List<SelectListItem> Categories { get; set; }
}

View

@for (var i = 0; i < Model.Categories.Count; i++)
{
    @Html.CheckBoxFor(model => model.Categories[i].Selected)
    @Html.HiddenFor(model => model.Categories[i].Text)
    @Html.HiddenFor(model => model.Categories[i].Value)
}

Controller

[HttpPost]
public ActionResult Index(MyModel model)
{
    if (ModelState.IsValid)
    {
        string selectedCategories = string.Join(",", 
                                    model.Categories.Where(x => x.Selected == true)
                                                    .Select(x => x.Text).ToList());

        // Save data to database, and redirect to Success page.

        return RedirectToAction("Success");
    }
}

Note:

There is a custom helper named CheckBoxListFor which should be considered to create checkbox list from List<T> property.

An example of the checkbox list implementation can be seen here.

Related problems:

Get comma-separated string from CheckboxList HTML Helper

Get Multiple Selected checkboxes in MVC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.