【问题标题】:How to get the value of the row that has been checked如何获取已检查的行的值
【发布时间】:2013-10-10 01:05:08
【问题描述】:

我有一个包含类别列表的模型,类别使用网页上的表格显示,我想为每个卷添加一个复选框,以便我可以选择多个角色并一次全部删除,我可以使用 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 行且没有选中,则操作的 IsSelected 参数仅填充代表每个角色的 false 值,但如果选中一行,则 IsSelected 参数将填充两个该行的 true 和 false 值。如何让任何选定的行只为真而不是真假?

【问题讨论】:

  • 你能添加一些代码和更多细节吗?
  • “它不工作”并没有告诉我们任何事情。您需要通过代码示例准确地告诉我们您做了什么,并说明如果您想要答案,它是如何不起作用的。
  • 当一个检查其他没有检查时,string[]中有多少项IsSelected?价值观是什么?
  • 4 项在字符串[] IsSelected 中,值为真、假、假、假。
  • 您能否将 Razor 视图文件生成的 HTML 输出添加到您的问题中?

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


【解决方案1】:

您是否尝试过类似的方法:

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

【讨论】:

    猜你喜欢
    • 2010-12-20
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多