View视图:
@using Droplets.WebCode; @{ Layout = null; ViewBag.Title = "WenZhangGuanLi"; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>文章管理</title> <link href="~/Content/easyui.css" rel="stylesheet" /> <link href="~/Content/icon.css" rel="stylesheet" /> @*<link href="~/Content/demo.css" rel="stylesheet" />*@ <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script src="~/Scripts/jquery.easyui.min.js"></script> <script src="~/Scripts/easyui-lang-zh_CN.js"></script> <script type="text/javascript"> function edit1() { var row = $('#dg').datagrid('getSelected'); if (!row) { $.messager.alert('提示', '未选中任何行。'); return; } window.location.href = "/WenZhang/Edit/" + row.Id; } function add1() { window.location.href = "/WenZhang/Edit/0" } function delect1() { var row = $('#dg').datagrid('getSelected'); if (!row) { $.messager.alert('提示', '未选中任何行。'); return; } if (confirm("确定要删除?")) { url = "/WenZhang/Delete"; parameter = { id: row.Id }; $.post(url, parameter, function (data) { alert("删除成功!"); window.location = "/WenZhang/Index"; }); } } </script> </head> <body id="Qbody"> <div id="dlg-toolbar"> <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add1()">添加</a> <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="edit1()">编辑</a> <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delect1()">删除</a> </div> <table id="dg" title="文章信息" style="width:98%;height:390px" data-options=" rownumbers:true, singleSelect:true,autoRowHeight:false,pagination:true,pageSize:10," url="/WenZhang/JsonPost?id=@ViewBag.temp" > <thead> <tr> <th field="Id">Id</th> <th field="Title" width="50%">文章标题</th> <th field="Editor" width="25%">添加人</th> <th field="BrowseCount" width="25%">点击量</th> </tr> </thead> </table> @*<table id="dg" title="文章信息" class="easyui-datagrid" style="width:98%;height:390px" url="/WenZhang/JsonPost?id=@ViewBag.temp" pagination="true" toolbar="#dlg-toolbar" rownumbers="true" fitColumns="true" singleSelect="true"> <thead> <tr> <th field="Id">Id</th> <th field="Title" width="80">文章标题</th> <th field="Content" width="50">内容</th> <th field="Editor" width="20">添加人</th> <th field="CreateTime" width="50">创建时间</th> <th field="BrowseCount" width="20">点击量</th> @*<th field="LanMu2_Id" width="80">所属栏目</th> </tr> </thead> </table>*@ <script> function getData(){ var rows = []; for(var i=1; i<=800; i++){ var amount = Math.floor(Math.random()*1000); var price = Math.floor(Math.random()*1000); rows.push({ inv: 'Inv No '+i, date: $.fn.datebox.defaults.formatter(new Date()), name: 'Name '+i, amount: amount, price: price, cost: amount*price, note: 'Note '+i }); } return rows; } function pagerFilter(data){ if (typeof data.length == 'number' && typeof data.splice == 'function'){ // is array data = { total: data.length, rows: data } } var dg = $(this); var opts = dg.datagrid('options'); var pager = dg.datagrid('getPager'); pager.pagination({ onSelectPage:function(pageNum, pageSize){ opts.pageNumber = pageNum; opts.pageSize = pageSize; pager.pagination('refresh',{ pageNumber:pageNum, pageSize:pageSize }); dg.datagrid('loadData',data); } }); if (!data.originalRows){ data.originalRows = (data.rows); } var start = (opts.pageNumber-1)*parseInt(opts.pageSize); var end = start + parseInt(opts.pageSize); data.rows = (data.originalRows.slice(start, end)); return data; } $(function(){ $('#dg').datagrid({loadFilter:pagerFilter}).datagrid('loadData', getData()); }); </script> </body> </html>
Controller:
JsonPost部分:查询出的信息排列成列表
public ActionResult JsonPost() { DropletsEntities dl = new DropletsEntities(); var p = from t in dl.WenZhang join t2 in dl.LanMu2 on t.LanMu2_Id equals t2.Id select new { Id=t.Id, Title = t.Title, Editor = t.Editor, CreateTime = t.CreateTime, BrowseCount = t.BrowseCount, LanMu2_Id = t2.Name, }; return Json(p, JsonRequestBehavior.AllowGet); }
//EditPost部分:添加或修改(编辑)
[HttpPost] [ValidateInput(false)] public ActionResult EditPost(WenZhang wenzhang) { DropletsEntities dao = new DropletsEntities(); if (wenzhang.Id == 0) { wenzhang.CreateTime = DateTime.Now; dao.WenZhang.Add(wenzhang); } else { var temp = dao.WenZhang.FirstOrDefault(t => t.Id == wenzhang.Id); temp.Title = wenzhang.Title; temp.Editor = wenzhang.Editor; temp.LanMu2_Id = wenzhang.LanMu2_Id; temp.Content = wenzhang.Content; temp.PictureCaptions = wenzhang.PictureCaptions; } dao.SaveChanges(); dao.Dispose(); return RedirectToAction("Edit", new { id = wenzhang.Id }); }
//Delete部分为删除
[HttpPost] public ActionResult Delete(int id) { DropletsEntities dao = new DropletsEntities(); var q = dao.WenZhang.FirstOrDefault(t => t.Id ==id); dao.WenZhang.Remove(q); dao.SaveChanges(); return RedirectToAction("Index"); }
//三部分与View的Js相关联