应用Jquery Serach,分页时,数据表复选框条目返回null ViewModel

时间:2019-05-20 06:40:00

标签: jquery asp.net-mvc datatables

在使用Jquery数据表通过分页,搜索等过滤器对行进行过滤之后,我只需要将检查过的行传递给视图模型的HTTP post action方法,但是表会向视图模型返回null ...

<tbody>
                                    @{
                                        int count = 0;
                                        for (int i = 0; i < Model.animalAdaptionDetails.Count; i++)
                                        {
                                            count++;
                                            <tr>
                                                <td>@count</td>
                                                <td>@Html.CheckBoxFor(it => it.animalAdaptionDetails[i].Checked, new { Style = "vertical-align:3px}" })</td>
                                                <td>
                                                    @Html.HiddenFor(it => it.animalAdaptionDetails[i].AnimalId)
                                                    @Html.DisplayFor(it => it.animalAdaptionDetails[i].name)
                                                </td>
                                                <td>
                                                    @Html.HiddenFor(it => it.animalAdaptionDetails[i].amount)
                                                    @Html.DisplayFor(it => it.animalAdaptionDetails[i].amount)
                                                </td>

                                            </tr>
                                        }
                                    }
                                </tbody>

控制器动作方法和模型类

[HttpPost]
        public ActionResult Index(AdoptionDetails model)
        {
            if (ModelState.IsValid)
            {
                if (AdoptionRepository.InsertAdoptionDetails(model))
                {
                    return RedirectToAction("AdaptionDetails");
                }
            }
            return View(AdoptionRepository.GetAnimalDetails());
        }




 public class AdoptionDetails
    {

        public int userId { get; set; }

        [Required]
        [MinLength(3, ErrorMessage = "Please enter at least 3 characters")]
        public string FirstName { get; set; }

        [Required]
        public string LastName { get; set; }


        public List<AnimalAdaptionDetails> animalAdaptionDetails { get; set; }
    }

    public class AnimalAdaptionDetails
    {
        public int AdoptionId { get; set; }
        public int AnimalId { get; set; }
        public string name { get; set; }
        public int? amount { get; set; }
        public bool Checked { get; set; }
    }

我尝试过的jQuery数据表代码

$("#target").submit(function (event) {
                debugger;
                table = $('#datatable-responsive').dataTable();
                //alert("Handler for .submit() called.");
                table.fnFilter("");
                var oSettings = table.fnSettings();
                table._iDisplayLength = -1;
                table.fnDraw();
            });

1 个答案:

答案 0 :(得分:0)

[HttpPost]
        public ActionResult Index(AdoptionDetails model,AnimalAdaptionDetails[] animalAdaptionDetails )
        {
            if (ModelState.IsValid)
            {
                if (AdoptionRepository.InsertAdoptionDetails(model))
                {
                    return RedirectToAction("AdaptionDetails");
                }
            }
            return View(AdoptionRepository.GetAnimalDetails());
        }