I posted the same question on the Datatables forum but they could not help me: https://datatables.net/forums/discussion/80946/searchpanes-not-working-correctly-with-razor-mvc#latest
I investigated a little bit more after posting so I write it down here again.
I am using Datatables with a .Net Framework 4.8 MVC application. The data (or part of it) of the table is lost when searching and submitting the Razor page to the Controller.
The problem is in a large legacy application but I created a simple app to pin down the problem. Here is the Razor page:
@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>
}
This is the 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 code is this:
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)
{
// model.CaseList is null here if I searched by "Case"
return View("Index", ViewModel);
}
}
}
When searching by "Case" like in the screenshot, the model.Caselist is null! When searching by "Text", it works ok (model.Caselist contains two rows).
