public class ShenModel
{
public int Id { get; set; } //审核编号
public string Name { get; set; } //提交人
public string Remark { get; set; } //审核内容
public DateTime STime { get; set; } //审核时间
public string SName { get; set; } //审核人
public int SBit { get; set; } //状态
public SectionInfo SectionInfo { get; set; }//外键
public int SectionInfoId { get; set; } //部门编号
}
public class SectionInfo
{
public int Id { get; set; } //部门编号
public string BName { get; set; } //部门名称
}
public class PageModel
{
//分页
public List<ShenModel> list { get; set; }
public int PageCount { get; set; }
}
namespace DAL
{
public class ShDBHelper : DbContext
{
public DbSet<SectionInfo> SectionInfo { get; set; }
public DbSet<ShenModel> ShenModel { get; set; }
}
}
namespace UI.Controllers
{
public class SectionController : ApiController
{
//实例化ShDBHelper
ShDBHelper dh = new ShDBHelper();
#region 显示
[HttpGet]
public PageModel Show(string name, int pageIndex = 1, int pageSize = 2)
{
PageModel page = new PageModel();
if (name == null)
{
name = "";
}
page.list = dh.ShenModel.Include("SectionInfo")
.Where(x => x.Name.Contains(name))
.OrderBy(x => x.Id)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.ToList();
//总计记录数
var count = dh.ShenModel.Include("SectionInfo")
.Where(x => x.SName.Contains(name)).Count();
//计算总页数
page.PageCount = count / pageSize + (count % pageSize == 0 ? 0 : 1);
return page;
}
#endregion
#region 添加
public int PostShen(ShenModel mf)
{
dh.ShenModel.Add(mf);
return dh.SaveChanges();
}
#endregion
#region 删除
[HttpDelete]
public int DeleteShen(int id)
{
var str = dh.ShenModel.Find(id);
dh.ShenModel.Remove(str);
return dh.SaveChanges();
}
#endregion
#region 修改状态
[HttpPut]
public int Upd(int id)
{
var str = dh.ShenModel.Find(id);
str.SBit = 1;
return dh.SaveChanges();
}
#endregion
#region 反填
[HttpGet]
public ShenModel Find(int id)
{
return dh.ShenModel.Include("SectionInfo").FirstOrDefault();
}
#endregion
#region 修改
[HttpPut]
public int Update(ShenModel mf)
{
dh.ShenModel.Attach(mf);
dh.Entry(mf).State = System.Data.Entity.EntityState.Modified;
return dh.SaveChanges();
}
#endregion
//实例化文件帮助类
APIFileHelp help = new APIFileHelp();
#region 导出
public void Export()
{
List<ShenModel> list = dh.ShenModel.Include("SectionInfo")
.Where(x => x.SBit == 2).ToList();
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("Id", "序号");
dic.Add("Name", "申请人");
dic.Add("SectionInfo.BName", "部门名称");
dic.Add("Remark", "申请描述");
dic.Add("STime", "申请时间");
dic.Add("SName", "审核人");
help.ExportExcel<ShenModel>("Shenqing.xls", list, dic);
}
#endregion
}
}
public class ShenController : ApiController
{
//实例化ShDBHelper
ShDBHelper dh = new ShDBHelper();
#region 绑定下拉列表
[HttpGet]
public List<SectionInfo> Sections()
{
return dh.SectionInfo.ToList();
}
#endregion
}
<script>
function dataLoad(index) {
//获取查询数据
var name = $("#name").val();
$.ajax({
url: 'http://localhost:44367/api/Section/Show',
type: 'get',
data: { name: name, PageIndex: index, pageSize: 2 },
dataType: 'JSON',
success: function (data) {
$("#tb").empty();
var list = data.list;
$(list).each(function () {
var ss =
" <tr>"
+ "<td>" + this.Id + "</td>"
+ "<td>" + this.Name + "</td>"
+ "<td>" + this.Remark + "</td>"
+ "<td>" + this.STime + "</td>"
+ "<td>" + this.SName + "</td>"
+ "<td>" + (this.SBit == 1 ? "通过" : "未审核") + "</td>"
+ "<td>" + (this.SBit == 2 ? '<input id="Button1" type="button" onclick="upd(' + this.Id + ')" value="通过" /> <input id="Button1" onclick="del(' + this.Id + ')" type="button" value="删除" />' : "") + "</td>"
+ "</tr>";
$("#tb").append(ss);
});
$("#page").empty();
for (var i = 1; i <= data.PageCount; i++) {
var s = '<input id="Button1" type="button" value="' + [i] + '" onclick="dataLoad(' + i + ')" />';
$("#page").append(s);
}
}
})
}
function del(id) {
if (confirm('是否删除?')) {
$.ajax({
url: 'http://localhost:44367/api/Section/DeleteShen/' + id,
type: 'delete',
data: {},
dataType: 'json',
success:
function (data) {
if (data > 0) {
alert("删除成功");
dataLoad();
} else {
alert("删除失败");
return;
}
}
})
}
}
function upd(id) {
$.ajax({
url: 'http://localhost:44367/api/Section/Upd/' + id,
type: 'put',
data: {},
dataType: 'Json',
success: function (data) {
if (data > 0) {
alert("修改成功");
dataLoad();
} else {
alert("修改失败");
return;
}
}
})
}
dataLoad(1);
</script>
<script>
function loadBind() {
$.ajax({
url: 'http://localhost:44367/api/Shen/Sections',
type: 'get',
data: {},
dataType: 'json',
success:
function (data) {
$("#s1").empty();
$(data).each(function () {
$("#s1").append(
"<option value='" + this.Id + "'>" + this.BName + "</option>"
);
})
}
})
}
function save() {
var mf = {};
mf.Name = $("#name").val();
mf.Remark = $("#neirong").val();
mf.STime = $("#time").val();
mf.SName = $("#sname").val();
mf.SBit = 2;
mf.SectionInfoId = $("#s1").val();
$.ajax({
url: 'http://localhost:44367/api/Section/PostShen',
type: 'post',
data: mf,
dataType: 'json',
success:
function (data) {
if (data > 0) {
alert('添加成功');
location.href = '/Shen/Show';
} else {
alert('添加失败');
return;
}
}
})
}
loadBind();
</script>