1.列表的增删改查(模板+代码部分)
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="lib/html5shiv.js"></script>
<script type="text/javascript" src="lib/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="~/static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="~/static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="~/lib/Hui-iconfont/1.0.8/iconfont.css" />
<link rel="stylesheet" type="text/css" href="~/static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="~/static/h-ui.admin/css/style.css" />
<link rel="stylesheet" href="~/layui/css/layui.css" />
<!--[if IE 6]>
<script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>用户管理</title>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont"></i> 首页 <span class="c-gray en">></span> 用户中心 <span class="c-gray en">></span> 用户管理 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont"></i></a></nav>
<div class="pd-20">
<input type="text" id="UserName" class="input-text" style="width:250px" placeholder="输入会员名称" name=""><button id="getLike" type="submit" class="btn btn-success" id="" name=""><i class="icon-search"></i> 搜用户</button>
</div>
<table class="layui-hide" id="test" lay-filter="demo"></table>
<script id="barDemo" type="text/html">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<div class="cl pd-5 bg-1 bk-gray mt-20">
<span class="l_f">
<a href="javascript:ovid()" id="Competence_add" class="btn btn-warning" title="添加用户"><i class="fa fa-plus"></i> 添加用户</a>
</span>
</div>
<div id="pageNav" class="pageNav">123</div>
<div id="Competence_add_style" style="display:none">
<div class="Competence_add_style">
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 名称 </label>
<div class="col-sm-9"><input type="text" id="Name" placeholder="" name="名称" class="col-xs-10 col-sm-5" /></div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 密码 </label>
<div class="col-sm-9"><input type="text" id="Password" placeholder="" name="密码" class="col-xs-10 col-sm-5" /></div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 邮箱 </label>
<div class="col-sm-9"><input type="text" id="Email" placeholder="" name="邮箱" class="col-xs-10 col-sm-5" /></div>
</div>
</div>
</div>
<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="~/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="~/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="~/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="~/static/h-ui.admin/js/H-ui.admin.js"></script>
<script src="~/layui/layui.js" type="text/javascript"></script>
<!--/_footer 作为公共模版分离出去-->
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="~/lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="~/lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="~/lib/laypage/1.2/laypage.js"></script>
</body>
</html>
<script type="text/javascript">
layui.use('table', function () {
var table = layui.table
, form = layui.form; //启用
table.render({
elem: '#test'
, url: '/User/Display/'
, cellMinWidth: 80
, cols: [[
{ type: 'checkbox' }
, { field: 'Id', width: 180, align: 'center', title: 'Id', sort: true }
, { field: 'Name', width: 180, align: 'center', title: '用户名' }
, { field: 'Password', width: 180, align: 'center', title: '密码' }
, { field: 'Email', width: 180, align: 'center', title: '邮箱' }
, { fixed: 'right', title: '操作', width: 208, align: 'center', toolbar: '#barDemo' }
]]
, page: true
, id: 'testReload'
});
$("#getLike").click(function () {
var table = layui.table;
//获取页面的查询条件
var UserName = $("#UserName").val();
//上述方qq1法等价于
table.reload('testReload', {
where: { //设定异步数据接口的额外参数,任意设
UserName: UserName,
//…
}
});
});
table.on('tool(demo)', function (obj) {
var AdminUser = obj.data;
var ID = AdminUser.Id;
if (obj.event === 'del') {
//删除
layer.confirm('确定删除吗?', function (index) {
$.ajax({
url: "/User/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') {
$("#ID").val(AdminUser.Id);
$("#Name").val(AdminUser.Name);
$("#Password").val(AdminUser.Password);
$("#Email").val(AdminUser.Email);
layer.open({
type: 1,
title: '修改菜单',
maxmin: true,
shadeClose: false,
area: ['800px', ''],
content: $('#Competence_add_style'),
btn: ['提交', '取消'],
yes: function (index, layero) {
var num = 0;
var str = "";
$(".col-sm-9 input[type$='text'],#form_textarea").each(function (n) {
if ($(this).val() == "") {
layer.alert(str += "" + $(this).attr("name") + "不能为空!\r\n", {
title: '提示框',
icon: 0,
});
num++;
return false;
}
});
if (num > 0) { return false; }
else {
var user = {};
user.Id = ID;
user.Name = $("#Name").val();
user.Password = $("#Password").val();
user.Email = $("#Email").val();
$.ajax({
url: "/User/Addhh",
type: "post",
data: user,
success: function (result) {
if (result.Success) {
layer.msg("添加成功");
location.reload();//刷新页面
}
else {
layer.msg("添加失败", { icon: 0, time: 3000 });
}
}
})
}
}
})
}
})
});
$('#Competence_add').on('click', function () {
layer.open({
type: 1,
title: '添加菜单',
maxmin: true,
shadeClose: false,
area: ['800px', ''],
content: $('#Competence_add_style'),
btn: ['提交', '取消'],
yes: function (index, layero) {
var num = 0;
var str = "";
$(".col-sm-9 input[type$='text'],#form_textarea").each(function (n) {
if ($(this).val() == "") {
layer.alert(str += "" + $(this).attr("name") + "不能为空!\r\n", {
title: '提示框',
icon: 0,
});
num++;
return false;
}
});
if (num > 0) { return false; }
else {
var user = {};
user.Name = $("#Name").val();
user.Password = $("#Password").val();
user.Email = $("#Email").val();
$.ajax({
url: "/User/Addhh",
type: "post",
data: user,
success: function (result) {
if (result.Success) {
layer.msg("添加成功");
location.reload();//刷新页面
}
else {
layer.msg("添加失败", { icon: 0, time: 3000 });
}
}
})
}
}
})
})
</script>
2.在实体层中添加PageListResult类
public class PageListResult<T>
{
public int code { get; set; }
public string msg { get; set; }
public int count { get; set; }
public List<T> data { get; set; }
}
3.务逻辑层创建UserService类:
public class UserService : BaseService<AdminUser>, IDenpendecy
{
}
控制器:
public class UserController : Controller
{
public UserService user = new UserService();
#region 显示查询
public ActionResult Display(int page, int limit, string UserName)
{
//PageListResult类
PageListResult<AdminUser> pageListResult = new PageListResult<AdminUser>();
//设值
pageListResult.code = 0;
pageListResult.msg = string.Empty;
//数据总数
int count = 0;
//where条件(ling表达式)
Expression<Func<AdminUser, bool>> Wherelambda = c => true;
if (!string.IsNullOrEmpty(UserName))
{
Wherelambda = a => a.Name.Contains(UserName);
}
//设置OrderBy排序的字段
Expression<Func<AdminUser, int>> OrderBylambda = c => c.Id;
//调用QuerypageList方法查询数据
pageListResult.data = user.QuerypageList(page, limit, Wherelambda, OrderBylambda, out count);
//返回结果
return Json(pageListResult, JsonRequestBehavior.AllowGet);
}
#endregion
#region 删除
public ActionResult Delete(AdminUser adminUser)
{
//Operate类
Operate operate = new Operate();
//调用DELETE方法删除数据
operate.Success = user.DELETE(adminUser);
return Json(operate);
}
#endregion
#region 添加/修改
public ActionResult Addhh(AdminUser adminUser)
{
//Operate类
Operate operate = new Operate();
//判断Id,是修改还是添加
if (adminUser.Id != 0)
{
//调用Update方法修改数据
operate.Success = user.Update(adminUser);
}
else
{
//调用Add方法添加数据
operate.Success = user.Add(adminUser);
}
return Json(operate);
}
#endregion
}