Controller
using Datatables.datatablesCs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
namespace Datatables.Controllers
{
public class UserTbController : Controller
{
testdbEntities db = new testdbEntities();
//
// GET: /UserTb/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult GetData(FormCollection collection)
{
int start = Convert.ToInt16(Request.Form["start"]);
int length= Convert.ToInt16(Request.Form["length"]);
List<UserTb> list = db.UserTb.OrderBy(p=>p.id).Skip(start).Take(length).ToList();
var aa = new { iTotalRecords = db.UserTb.Count(), iTotalDisplayRecords = db.UserTb.Count(), aaData = list };
//sEcho = 2,
// Thread.Sleep(2000);
return Json(aa, JsonRequestBehavior.AllowGet);
}
public ActionResult List()
{
//延时加载
IQueryable<UserTb> users = db.UserTb.AsQueryable<UserTb>();
if (Request["start"] != null)
{
var parser = new DataTableParser<UserTb>(Request, users);
return Json(parser.Parse());
}
return Json(new { aaData = users });
}
//
// GET: /UserTb/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /UserTb/Create
public ActionResult Create()
{
return View();
}
//
// POST: /UserTb/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
UserTb ut = new UserTb {
UerName=collection["UerName"],
UserNo = collection["UserNo"],
Age =int.Parse( collection["Age"])
};
db.UserTb.Add(ut);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /UserTb/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /UserTb/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /UserTb/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /UserTb/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
JS:
$(function () {
$(‘#list‘).dataTable({
"oLanguage": {//下面是一些汉语翻译
"sProcessing": "处理中...",
"sLengthMenu": "显示 _MENU_ 项结果",
"sZeroRecords": "没有匹配结果",
"sInfo": "显示第 _START_ 至 _END_ 项结果,共 _TOTAL_ 项",
"sInfoEmpty": "显示第 0 至 0 项结果,共 0 项",
"sInfoFiltered": "(由 _MAX_ 项结果过滤)",
"sInfoPostFix": "",
"sSearch": "搜索:",
"sUrl": "",
"sEmptyTable": "暂无数据",
"sLoadingRecords": "数据加载中...",
"sInfoThousands": ",",
"oPaginate": {
"sFirst": "首页",
"sPrevious": "上页",
"sNext": "下页",
"sLast": "末页"
},
"oAria": {
"sSortAscending": ": 以升序排列此列",
"sSortDescending": ": 以降序排列此列"
}
},
"processing": true,
//"iDisplayLength": 10,//每页显示10条数据
//"iDisplayStart":1,
"serverSide": true,
"sPaginationType": "full_numbers",
"dom": ‘<"#toolsbar"><"top"i>rt<"bottom"flp><"clear">‘,
// "dom": ‘<"top"i>rt<"bottom"flp><"clear">‘,
"ajax": {
"url": "/UserTb/GetData", //List
"type": "POST",
"data": function (d) {
d.ttStart = "iDisplayStart",
d.ttyLength = "iDisplayLength",// + " 23:59:59",
d.uu=89
//d.keyword = "sSearch"
}
},
"columns": [
{ "data": "id" },
{ "data": "UserNo" },
{ "data": "UerName" },
{ "data": "Age" }
],
initComplete: initComplete
});
});
function initComplete(data) {
// $("#toolsbar").css("float", "left");
$("#toolsbar").append(‘<div style="float: left;"><input type="text"/></div>‘);
}
view:
@{
ViewBag.Title = "Index";
}
@*<script src="~/Scripts/dataTables.buttons.min.js" type="text/javascript"></script>*@
<h2>Index</h2>
<div>
<table id="list" class="gridtable" style="width:100%;">
<thead>
<th>ID</th>
<th>USERNO</th>
<th>USERNAME</th>
<th>AGE</th>
</thead>
<tbody>
</tbody>
</table>
</div>