0

I have a model that contain a list of category, the categories are displayed using a table on a web page, I want to add a check box to each roll so that I can select multiple role and delete them all at once, I could do that using Knockout, but I want to do it without using Knockout. How do I go about getting the row value especially the ID column for row that are checked, I tried following the answer gave here Getting values of check-box from formcollection in asp.net mvc but it is not working. Any Ideas Thanks.

Below is a sample code for my model

public class Category
{
    public Category()
    {
        CreatedOn = DateTime.Now;
    }

    /// <summary>
    /// Category unique identification
    /// </summary>
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    /// <summary>
    /// Name of the category
    /// </summary>
    [Required(AllowEmptyStrings = false, ErrorMessage = "Can't be left empty")]
    [StringLength(500, MinimumLength = 2, ErrorMessage = "An Error has occured, check your input and try agian")]
    public string Name { get; set; }

Below is the code for my view model

public class CategoryViewModel
{
    public CategoryViewModel(UnitOfWork _unitofWork)
    {
        CategoryList = _unitofWork.CategoryRepo.GetAll().ToDictionary(v => v, v => IsSelected);
    }

    public IDictionary<Category, bool> CategoryList { get; private set; }
    public bool IsSelected {get; private set; }
}

for my view

<table class="table table-striped table-hover" id="sample_1">
    <thead>
        <tr>
            <th style="width: 8px;">
                <input type="checkbox" class="group-checkable" data-set="#sample_1 .checkboxes" />
            </th>
            <th>ID</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody>
       @foreach (var item in Model.CategoryList)
       {
           <tr class="odd gradeX">
              <td>
                  @Html.CheckBoxFor(m => m.IsSelected, item.value)
              </td>
              <td>@item.ID</td>
              <td>@item.Name</td>
           </tr>
       }
    </tbody>
</table>

and my controller action

public ActionResult DeleteCat(string[] IsSelected)

For example if there are 3 row and none is checked the IsSelected parameter of the Action is populated with just the value of false representing each role, but if one row is checked the IsSelected parameter is populated with both the value of true and false for that row. How do I get just true for any selected row instead of getting true and false?

Community
  • 1
  • 1

1 Answers1

0

Have you tried something like:

@for (int i = 0; i < Model.CategoryList.Length, i++)
{
    <tr class="odd gradeX">
        <td>
            @Html.HiddenFor(m => Model.CategoryList[i].ID)
            @Html.CheckBoxFor(m => Model.CategoryList[i].IsSelected)
        </td>
        <td>@item.ID</td>
        <td>@item.Name</td>
    </tr>
 }
user247702
  • 23,641
  • 15
  • 110
  • 157
Slicksim
  • 7,054
  • 28
  • 32