MVC单表新增
开发工具:Visual Studio 2017
关键技术:MVC JQuery Html5
撰写时间:2019-4-28
今天在这里给大家分享的功能是MVC单表新增
效果图:
点击新增按钮可以打开新增模态窗口
将需要新增的数据填入文本框,点击保存按钮保存
新增成功后就弹出提示“新增成功”,则失败就弹出提示“新增失败”
新增成功后的数据
MVC代码:
public ActionResult InsertEmployee(PW_Employee Employee)
{
ReturnJson msg = new ReturnJson();
try
{
var hasEmployee = (from tb in myModels.PW_Employee
where tb.employeeName == Employee.employeeName || tb.telphone == Employee.telphone
select tb).Count();
if (hasEmployee == 0)
{
myModels.PW_Employee.Add(Employee);
if (myModels.SaveChanges() > 0)
{
msg.State = true;
msg.Text = "新增成功!";
}
else
{
msg.Text = "新增失败!";
}
}
else
{
msg.Text = "该员工已存在,不需要重复添加!";
}
}
catch (Exception)
{
msg.Text = "数据异常!";
}
return Json(msg, JsonRequestBehavior.AllowGet);
}
Html代码:
新增按钮
<button class="layui-btn" id="openModal">新增</button>
JQuery代码:
保存按钮
$("#save").click(function () {
var ID = $("#employeeID").val();
var Num = $("#employeeNum").val();
var Name = $("#employeeName").val();
var phone = $("#telphone").val();
var address = $("#address").val();
//判断NoInsert等于true还是等于false
//等于true执行新增保存方法
//等于false执行修改保存方法
if (NoInsert) {
if (Num != "" && Name != "" && phone != "" && address != "") {
$.get("/Main/InsertEmployee", { employeeNum: Num, employeeName: Name, telphone: phone, address: address }, function (msg) {
if (msg.State) {
layer.alert(msg.Text, { icon: 1, title: '提示' });
$("#modalEmployee").modal('hide');
TabEmployee.reload();
} else {
layer.alert(msg.Text, { icon: 0, title: '提示' });
}
});
} else {
layer.alert("信息不能为空", { icon: 0, title: '提示' });
}
} else {
}
});