如何获取已检查行的值

时间:2013-10-10 01:05:09

标签: c# asp.net-mvc c#-4.0 asp.net-mvc-4 razor

我有一个包含类别列表的模型,使用网页上的表格显示类别,我想为每个卷添加一个复选框,以便我可以选择多个角色并一次删除它们,我可以使用Knockout做到这一点,但我想在不使用Knockout的情况下做到这一点。我如何获取行值,特别是检查行的ID列,我尝试按照这里给出的答案Getting values of check-box from formcollection in asp.net mvc但它不起作用。任何想法谢谢。

以下是我的模型的示例代码

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

以下是我的视图模型的代码

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

我的观点

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

和我的控制器动作

public ActionResult DeleteCat(string[] IsSelected)

例如,如果有3行且未选中,则仅使用表示每个角色的false值填充Action的IsSelected参数,但如果选中了一行,则IsSelected参数为填充了该行的true和false值。我如何才能获得任何选定的行而不是真实和假?

1 个答案:

答案 0 :(得分:0)

你有没有试过像:

@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>
 }
相关问题