简体   繁体   English

如何获取已检查的行的值

[英]How to get the value of the row that has been checked

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. 我有一个包含类别列表的模型,使用网页上的表格显示类别,我想为每个卷添加一个复选框,以便我可以选择多个角色并一次将其全部删除,我可以使用Knockout,但我想不使用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. 我该如何获取行值,尤其是要检查的行的ID列,我尝试按照此处给出的答案从asp.net mvc中的formcollection获取复选框的值,但它不起作用。 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. 例如,如果有3行而没有选中任何行,则IsSelected代表每个角色的false值填充该Action的IsSelected参数,但是如果选中一行,则IsSelected填充true和false值的IsSelected参数行。 How do I get just true for any selected row instead of getting true and false? 如何对任何选定的行仅求真而不是对和错?

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>
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM