我目前正在使用来自MVC控制器的JQuery标准数据库。
我的模型在首次加载时包含数千条记录。加载过程需要几秒钟,并且页面在加载过程中会极长地转动。在所有加载后,可设置为每页10条记录。
所以我决定使用数据服务器端处理。减少第一个加载时间。如何让它工作,实体框架工作在mvc?
public ActionResult ProductList() {
ProductModel model = new ProductModel();
model = db.productModels.ToList();
return View(model)
}
<table>
@{
<th>columns</th>
foreach (var item in productmodel)
{
//loop for my logic for all products
<td>@item</td>
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
这就是我最终如何做到这就是我想要的。 归功于这个人https://www.codeproject.com/Tips/1011531/Using-jQuery-DataTables-with-Server-Side-Processin
$(document).ready(function () {
$('#table_id').DataTable({
"processing": true,
"serverSide": true,
"info": true,
"pageLength": 3,
"lengthMenu": [[10, 20, 50, -1], [10, 20, 50, "All"]],
"columns": [
{ "data": "id", "orderable": true },
{ "data": "productName", "orderable": true
},
],
"ajax":"@Url.Action("GetProductList","Home")"
});
});
public struct DataTableData
{
public int draw { get; set; }
public int recordsTotal { get; set; }
public int recordsFiltered { get; set; }
public List<ProductTable> data { get; set; }
}
private int SortString(string s1, string s2, string sortDirection)
{
return sortDirection == "asc" ? s1.CompareTo(s2) : s2.CompareTo(s1);
}
public JsonResult GetProductList(int draw, int start, int length)
{
var model = db.ProductTables.ToList();
DataTableData dt = new DataTableData();
string search = Request.QueryString["search[value]"];
string ha= Request.QueryString["columns[0][orderable]"];
string order = Request.QueryString["order"];
string ord= Request.QueryString["order[0][dir]"];
if (!string.IsNullOrEmpty(search))
{
dt.data = model.Where(x=>x.productName.Contains(search)).ToList();// model.GetRange(start, Math.Min(length, model.Count - start));
}
dt.data=model.Skip(start).Take(length).ToList();
if (!string.IsNullOrEmpty(ord))
{
dt.data.Sort((x, y) => SortString(x.productName, y.productName, ord));
}
dt.draw = draw;
dt.recordsTotal = model.Count;
dt.recordsFiltered = model.Count;
return Json(dt, JsonRequestBehavior.AllowGet);
}