ok-admin框架,第五天用户更新

 

 

 

 


@model StudentSys.Models.SysUser
@{
ViewBag.Title = "更新用户";
Layout = null;
}

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新用户</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="/css/oksub.css">
<script type="text/javascript" src="/lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
<!--form表单-->
<form class="layui-form ok-form" lay-filter="filter">
<div class="layui-form-item">
<label class="layui-form-label">用户名</label>
<div class="layui-input-block">
<input type="hidden" name="id" class="layui-input"
value="@Model.id">


<input type="text" name="username" placeholder="请输入用户名" disabled="disabled" autocomplete="off" class="layui-input"
lay-verify="required" value="@Model.username">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">姓名</label>
<div class="layui-input-block">
<input type="text" name="Realname" placeholder="请输入真实姓名" autocomplete="off" class="layui-input"
lay-verify="required" value="@Model.Realname">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">手机号码</label>
<div class="layui-input-block">
<input type="text" name="phone" placeholder="请输入手机号码" autocomplete="off" class="layui-input" lay-verify="phone" value="@Model.phone">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密码</label>
<div class="layui-input-block">
<input type="password" name="PassWord" placeholder="请输入密码" autocomplete="off" class="layui-input"
lay-verify="required" value="@Model.PassWord">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">部门</label>
<div class="layui-input-block">
@*<input type="text" name="depart" placeholder="请输入部门" autocomplete="off" class="layui-input"
lay-verify="required" value="@Model.depart">*@

<select name="depart" id="depart">

</select>
</div>
</div>
@if (Session["RID"].ToString() == "1")
{
<div class="layui-form-item">
<label class="layui-form-label">角色</label>
<div class="layui-input-block">
<select name="RID" id="RID">
@*<option value="0">系统管理员</option>
<option value="1">普通员工</option>*@
</select>
</div>
</div>
}
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="edit">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</div>
<!--js逻辑-->
<script src="/lib/layui/layui.js"></script>
<script src="~/Scripts/jquery-3.4.1.js"></script>
<script>

 

layui.use(['form', 'upload', 'layer'], function() {
var form = layui.form;
//检查项目添加到下拉框中
$.ajax({
url: '/UserList/InitRole',
dataType: 'json',
type: 'get',
success: function(data) {
$.each(data, function(index, item) {
$('#RID').append(new Option(item.name, item.id));// 下拉菜单里添加元素

});
$("#RID").val(@Model.RID);//绑定原来的角色
layui.form.render("select");
//重新渲染 固定写法
}
})

});


layui.use(['form', 'upload', 'layer'], function() {
var form = layui.form;
//检查项目添加到下拉框中
$.ajax({
url: '/UserList/InitDepart',
dataType: 'json',
type: 'get',
success: function(data) {
$.each(data, function(index, item) {
$('#depart').append(new Option(item.name, item.id));// 下拉菜单里添加元素

});
$("#depart").val(@Model.depart);
layui.form.render("select");
//重新渲染 固定写法
}
})

});


let initData;

function initForm(data) {
let jsonString = JSON.stringify(data);
console.log("jsonString:" + jsonString);
initData = JSON.parse(jsonString);
}

layui.use(["element", "form", "laydate", "okLayer", "okUtils"], function () {
let form = layui.form;
let laydate = layui.laydate;
let okLayer = layui.okLayer;
let okUtils = layui.okUtils;
okLoading.close();
form.val("filter", initData);

/*laydate.render({elem: "#birthday", type: "datetime"});*/

//form.verify({
// birthdayVerify: [/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))(\s(([01]\d{1})|(2[0123])):([0-5]\d):([0-5]\d))?$/, '日期格式不正确']
//});

form.on("submit(edit)", function (data) {
okUtils.ajax("/UserList/Update", "Post", data.field, true).done(function (response) {

okLayer.greenTickMsg("编辑成功", function () {
parent.layer.close(parent.layer.getFrameIndex(window.name));
/* window.location.href = "/UserList/LoadPageList";*/
});
}).fail(function (error) {
console.log("编辑用户fail:" + error)
});
return false;
});
})</script>
</body>
</html>

 

 

 

/// <summary>
/// 更新页面
/// </summary>
/// <returns></returns>
public ActionResult Update()
{
int id = int.Parse(Request["id"]);
ViewData["uid"] = id;
YuanGongDb dbcontext = new YuanGongDb();
var data = dbcontext.SysUser.FirstOrDefault(o => o.id ==id);
return View(data);
}

/// <summary>
/// 更新用户
/// </summary>
/// <param name="_user"></param>
/// <returns></returns>
[HttpPost]
public string Update(SysUser _user)
{

YuanGongDb dbcontext = new YuanGongDb();
ResponseData res = new ResponseData();//返回的数据格式code,msg
var entity = dbcontext.Set<SysUser>().FirstOrDefault(o=>o.id==_user.id);
entity.username = _user.username;
entity.Realname = _user.Realname;
entity.PassWord = _user.PassWord;
entity.phone = _user.phone;
entity.depart = _user.depart;
if (Session["RID"].ToString() =="1")
{
entity.RID = _user.RID;
}
if (dbcontext.SaveChanges()>0)
{
res.code = 0;
res.msg = "更新成功";
var data = JsonConvert.SerializeObject(res);
return data;
}
else
{
res.code = 1;
res.msg = "更新失败";
var data = JsonConvert.SerializeObject(res);
return data;
}
}

posted @ 2022-05-22 10:05  .net&new  阅读(123)  评论(0编辑  收藏  举报