0

カテゴリのリストを含むモデルがあり、カテゴリは Web ページのテーブルを使用して表示されます。複数のロールを選択して一度に削除できるように、各ロールにチェック ボックスを追加したいと考えています。 Knockout を使ってのことですが、Knockout を使わずにやりたいです。行の値、特にチェックされている行の ID 列を取得するにはどうすればよいですか。こちらの回答に従ってみました。任意のアイデアありがとう。

以下は私のモデルのサンプルコードです

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、Action のパラメーターには各ロールを表す false の値だけが取り込まれますが、1 つの行がチェックされている場合、IsSelectedパラメーターにはその行の true と false の両方の値が取り込まれます。true と false を取得する代わりに、選択した行に対して true を取得するにはどうすればよいですか?

4

1 に答える 1