Razor視圖支持:總CRUD操作
@{
ViewBag.Title = "Home Page";
}
<table id="tbl"></table>
<div id="pager"></div>
@section scripts{
<link href="~/Content/Theme/ui.jqgrid.css" rel="stylesheet" />
<link href="~/Content/Theme/jquery-ui.min.css" rel="stylesheet" />
<script src="~/Scripts/jqGrid/jquery.jqGrid.js"></script>
<script src="~/Scripts/jqGrid/grid.inlinedit.js"></script>
<script src="~/Scripts/jqGrid/grid.locale-en.js"></script>
<script src="~/Scripts/jqGrid/jquery.sortable.js"></script>
<script>
$(function() {
var lastsel;
$("#tbl").jqGrid({
url: "/Home/GetData",
mtype: "Get",
datatype: "Json",
colNames: ["ID", "Name", "Address", "Mobile", "Salary"],
colModel: [
{ name: 'id', index: 'id', editable: false, align: 'center' },
{ name: 'name', index: 'name', editable: true },
{ name: 'address', index: 'address', editable: true },
{ name: 'mobile', index: 'mobile', editable: true },
{ name: 'salary', index: 'salary', editable: true }
],
loadonce: true,
pager: "#pager",
rowNum: 20,
height:"100%",
onSelectRow: function (id) {
if (id && id !== lastsel) {
$("#tbl").restoreRow(lastsel);
$("#tbl").editRow(id, true);
lastsel = id;
}
},
caption: "jqGrid",
editurl: "/Home/EditData",
viewrecords: true,
sortorder: "desc",
sortname: "id",
}).navGrid("#pager", { edit: false, add: false, del: true, refresh: false, search: false },
{},
{},
{
url: "/Home/DelData",
mtype: "Post",
delData: "row_id_s",
}).inlineNav("#pager", {
add: true,
addParams: {
addRowParams: {
url: "/Home/AddData",
mtype: "Post"
}
}
});
});
</script>
}
MVC代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using jqGrid_Exam2.Models;
using System.Data.Entity;
namespace jqGrid_Exam2.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult GetData()
{
DBcontext db = new DBcontext();
var data = db.EmployeeTbls.ToList<EmployeeTbl>();
return Json(data,JsonRequestBehavior.AllowGet);
}
[HttpPost]
public void EditData(EmployeeTbl emp)
{
DBcontext db = new DBcontext();
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
}
[HttpPost]
public void AddData(EmployeeTbl emp)
{
DBcontext db = new DBcontext();
db.EmployeeTbls.Add(emp);
db.SaveChanges();
}
[HttpPost]
public void DelData(string id)
{
DBcontext db = new DBcontext();
EmployeeTbl emp = db.EmployeeTbls.Find(int.Parse(id));
db.EmployeeTbls.Remove(emp);
db.SaveChanges();
}
}
}