You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jQuery DataTables搜索提交至ASP.NET MVC控制器时CaseList为空问题

DataTables SearchPanes与ASP.NET MVC Razor提交数据异常问题

我之前在DataTables论坛发布过相同问题但未得到解决,后续做了进一步调查,现重新描述问题:

在.NET Framework 4.8的MVC应用中使用DataTables,当通过SearchPanes搜索后提交Razor页面到控制器时,表格数据会出现丢失情况。为定位问题,我搭建了简易测试项目,相关代码如下:

Razor页面代码

@using MVCTestbench.Models
@using System.Collections.Generic

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/datatable")

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Styles.Render("~/Styles/datatable")

@{
    ViewBag.Title = "Home Page";
}

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {

        new DataTable('#CaseTable', {
            layout: {
                top1: {
                    searchPanes: {
                        viewTotal: true
                    }
                }
            }
        });
    });
</script>

@using (Html.BeginForm("Post", "Home", FormMethod.Post, new { id = "frmCases"}))
{
    <input type="submit" value="Save" />
    <br />

    <table id="CaseTable">
        <thead>
            <tr>
                <th>Id</th>
                <th>Case</th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.CaseList.Count; i++)
            {
                <tr>
                    <td>
                        @Model.CaseList[i].Id @Html.HiddenFor(m => m.CaseList[i].Id)
                    </td>
                    <td>
                        @Model.CaseList[i].Name @Html.HiddenFor(m => m.CaseList[i].Name)
                    </td>
                </tr>
            }
        </tbody>
    </table>
}

ViewModel代码

using System.Collections.Generic;

namespace MVCTestbench.Models
{
    public class CaseViewModel
    {
        public List<Case> CaseList { get; set; } = new List<Case>();
    }

    public class Case
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
}

Controller代码

using MVCTestbench.Models;
using System.Web.Mvc;

namespace MVCTestbench.Controllers
{
    public class HomeController : Controller
    {
        private CaseViewModel ViewModel
        {
            get
            {
                var model = new CaseViewModel();
                model.CaseList.Add(new Case { Id = "1", Name = "Text 1" });
                model.CaseList.Add(new Case { Id = "2", Name = "Text 2" });
                model.CaseList.Add(new Case { Id = "3", Name = "Case 3" });
                return model;
            }
        }

        public ActionResult Index()
        {
            return View(ViewModel);
        }

        [HttpPost]
        public ActionResult Post(CaseViewModel model)
        {
            // 当搜索"Case"时,此处model.CaseList为null
            return View("Index", ViewModel);
        }

    }
}

问题现象

  • 搜索“Case”时,控制器Post方法中的model.CaseList为null;
  • 搜索“Text”时,model.CaseList正常包含两条匹配的数据。

搜索Case时的界面截图

内容的提问来源于stack exchange,提问作者Petteri Fischer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 01:12:05