1创建项目点击MVC具体参照登录操作
2在表示层的控制器文件写代码
2.1添加和编辑一起,编辑需要从数据库获取数据放在页面上修改,所以要查询要在业务层创建一个类写查询代码
//查询单表用lamdam表达式
public class ModueService : BaseService<InfoManager>
{
public InfoManager SelectInfoMangers(int id)
{
return Query(a => a.ID == id);
}
}
2.1.1创建一个AdminUserController控制器(命名随意)
//添加
public JsonResult Add(InfoManager infoManager)
{
ModueService modueService = new ModueService();
OperateResult operateResult = new OperateResult();
operateResult.Success = modueService.Add(infoManager);
return Json(operateResult);
}
//修改
public JsonResult Update(InfoManager infoManager)
{
ModueService modueService = new ModueService();
OperateResult operateResult = new OperateResult();
operateResult.Success = modueService.Update(infoManager);
return Json(operateResult);
}
2.1.2再去Home控制器下的Add视图传值判断
public ActionResult articleadd(int id)
{
//UserIndex页面传一个id判断改添加操作还是修改操作
if (id != 0)
{
ModueService modueService = new ModueService();
//读取信息调用SelectUserInfo方法
ViewBag.info = modueService.SelectInfoMangers(id);
}
return View();
}
2.1.3页面代码Ajax
$(function () {
//提交点击事件
$("#submit").click(function () {
//实例化对象
var userInfo = {};
userInfo.ID = $("#id").val();
userInfo.Title = $("#articletitle").val();
userInfo.Sort = $("#sort").val();
userInfo.Source = $("#sources").val()
userInfo.Browser = $("#browser").val();
userInfo.Status = $("#status").val();
//判断Id是否为空,true则进行添加操作
if ($("#id").val() == 0) {
//温馨提示:是否添加用户
layer.confirm('是否添加用户', function (index) {
//ajax提交到后台进行添加用户操作
$.ajax({
type: "post",
url: "/AdminUser/Add",
data: userInfo,
success: function (operateResult) {
if (operateResult.Success) {
//温馨提示:添加成功
layer.msg('添加成功!', {
icon: 1,
time: 1000
});
} else {
//温馨提示:添加失败
layer.msg('添加失败!', {
icon: 1,
time: 1000
});
}
},
})
})
//如果Id是不为空,false则进行修改操作
} else {
//温馨提示:是否修改用户
layer.confirm('是否修改用户', function (index) {
//ajax提交到后台进行修改用户操作
$.ajax({
type: "post",
url: "/AdminUser/Update",
data: userInfo,
success: function (operateResult) {
//温馨提示:修改成功
if (operateResult.Success) {
layer.msg('修改成功!', {
icon: 1,
time: 1000
});
} else {
//温馨提示:添加失败
layer.msg('修改失败!', {
icon: 1,
time: 1000
});
}
}
})
})
}
})
3.删除在控制器代码
//删除单条数据
public JsonResult DELETE(InfoManager infoManager)
{
ModueService modueService = new ModueService();
OperateResult operateResult = new OperateResult();
operateResult.Success = modueService.DELETE(infoManager);
return Json(operateResult);
}
3.1页面代码
layui.use('table', function () {
var table = layui.table
, form = layui.form; //启用
table.render({
elem: '#test'
, url: '/AdminUser/GetMoudelList/'
, cellMinWidth: 80
, cols: [[
{ type: 'checkbox' }
, { field: 'ID', width: 180, align: 'center', title: 'Id', sort: true }
, { field: 'Title', width: 180, align: 'center', title: '标题' }
, { field: 'Sort', width: 180, align: 'center', title: '分类' }
, { field: 'Source', width: 180, align: 'center', title: '来源' }
, { field: 'Browser', width: 180, align: 'center', title: '浏览' }
, { field: 'Status', width: 180, align: 'center', title: '状态' }
, { fixed: 'right', title: '操作', width: 208, align: 'center', toolbar: '#barDemo' }
]]
, page: true
, id: 'testReload'
});
table.on('tool(demo)', function (obj) {
var AdminUser = obj.data;
var ID = AdminUser.ID;
if (obj.event === 'del') {
//删除
layer.confirm('确定删除吗?', function (index) {
$.ajax({
url: "/AdminUser/DELETE?Id=" + ID,
type: "Post",
success: function (data) {
if (data.Success) {
layer.msg('删除成功!', {
title: '提示框',
icon: 1,
time: 2000
}, function () {
location.reload();//刷新页面
layer.close(index);
});
}
else {
layer.msg('删除失败!', {
title: '提示框',
icon: 1,
time: 2000
});
}
}
});
});
} else if (obj.event === 'edit') {
window.location.href = "/Home/articleadd?id=" + ID;
};
})
})
4.查询
//分页查询
public JsonResult GetMoudelList(int page, int limit)
{
ModueService modueService = new ModueService();
ResultDataSet<InfoManager> resultDataSet = new ResultDataSet<InfoManager>();
resultDataSet.code = 0;
resultDataSet.msg = string.Empty;
resultDataSet.count = 0;
int count = 0;
List<InfoManager> modules = new List<InfoManager>();
Expression<Func<InfoManager, bool>> whereLambda = a =>true;
Expression<Func<InfoManager, int>> orderbyLambda = a => a.ID;
resultDataSet.data = modueService.QuerypageList(page, limit, whereLambda, orderbyLambda, out count);
return Json(resultDataSet,JsonRequestBehavior.AllowGet);
}
4.1页面查询代码
layui.use('table', function () {
var table = layui.table
, form = layui.form; //启用
table.render({
elem: '#test'
, url: '/AdminUser/GetMoudelList/'
, cellMinWidth: 80
, cols: [[
{ type: 'checkbox' }
, { field: 'ID', width: 180, align: 'center', title: 'Id', sort: true }
, { field: 'Title', width: 180, align: 'center', title: '标题' }
, { field: 'Sort', width: 180, align: 'center', title: '分类' }
, { field: 'Source', width: 180, align: 'center', title: '来源' }
, { field: 'Browser', width: 180, align: 'center', title: '浏览' }
, { field: 'Status', width: 180, align: 'center', title: '状态' }
, { fixed: 'right', title: '操作', width: 208, align: 'center', toolbar: '#barDemo' }
]]
, page: true
, id: 'testReload'
});
table.on('tool(demo)', function (obj) {
var AdminUser = obj.data;
var ID = AdminUser.ID;
if (obj.event === 'del') {
//删除
layer.confirm('确定删除吗?', function (index) {
$.ajax({
url: "/AdminUser/DELETE?Id=" + ID,
type: "Post",
success: function (data) {
if (data.Success) {
layer.msg('删除成功!', {
title: '提示框',
icon: 1,
time: 2000
}, function () {
location.reload();//刷新页面
layer.close(index);
});
}
else {
layer.msg('删除失败!', {
title: '提示框',
icon: 1,
time: 2000
});
}
}
});
});
} else if (obj.event === 'edit') {
window.location.href = "/Home/articleadd?id=" + ID;
};
})
})
</script>