- 打开模态框
HTML
@*<button class="layui-btn" id="openInsert">新增</button>*@
<button class="layui-btn" onclick="openInsert()">新增</button>
jquery
//$("#openInsert").click(function() {
// $("#formEmployee")[0].reset();
// $("#modalEmployee").modal();
//});
function openInsert() {
$("#formEmployee")[0].reset();//重置表单
$("#modalEmployee").modal();//打开模态框
}
第一种是用button 按钮获取它的id,给它加上onclicke事件
第二种则是直接给button 按钮直接加上onclicke事件,然后调用方法
- 控制器
public ActionResult InsertAcademe(PW_Employee sysEmployee)//传入参数sysEmployee
{
ReturnJson returnJson =new ReturnJson();
returnJson.State = false;
//判断页面传过来的数据是否为空
if
(!string.IsNullOrEmpty(sysEmployee.employeeName)
&&
!string.IsNullOrEmpty(sysEmployee.employeeNum)
&&
!string.IsNullOrEmpty(sysEmployee.telphone)
&&
!string.IsNullOrEmpty(sysEmployee.address))
{
//检查输入的数据和以前的是否有冲突
int intcount = (from tb in myModel.PW_Employee
where tb.employeeName == sysEmployee.employeeName.Trim() ||
tb.employeeNum == sysEmployee.employeeNum.Trim() ||
tb.telphone == sysEmployee.telphone.Trim() ||
tb.address == sysEmployee.address.Trim()
select tb).Count();
if (intcount==0)
{
//Linq 新增保存
myModel.PW_Employee.Add(sysEmployee);
int intR = myModel.SaveChanges();
if (intR > 0)
{
returnJson.State = true;
returnJson.Text = "新增成功";
}
else
{
returnJson.Text = "新增失败";
}
}
else
{
returnJson.Text = "和以前学院信息的重复!";
}
}
else
{
returnJson.Text = "数据不完整";
}
return Json(returnJson, JsonRequestBehavior.AllowGet);
}
- ReturnJson封装字段
public class ReturnJson
{
/// <summary>
/// 状态
/// </summary>
public bool State { get; set; }
/// <summary>
/// 状态码
/// </summary>
public string Code { get; set; }
/// <summary>
/// 文本
/// </summary>
public String Text { get; set; }
/// <summary>
/// 附加数据
/// </summary>
public object Object { get; set; }
}
- Jquery
function savaInsert() {
//获取input输入数据
var employeeNum = $("#employeeNum").val();
var employeeName = $("#employeeName").val();
var telphone = $("#telphone").val();
var address = $("#address").val();
//判断数据是否为空
if (employeeNum != '' && employeeNum != undefined
&&employeeName != '' && employeeName != undefined
&& telphone!= '' && telphone != undefined
&& address != '' && address != undefined
) {
$.post("/Main/InsertAcademe",//页面请求
{
employeeNum:employeeNum,
employeeName: employeeName,
telphone: telphone,
address: address
}, function (returnJson) {
if (returnJson.State == true) {
//关闭模态框
$("#modalEmployee").modal('hide');
//刷新表格
tabAcademe = layuiTable.reload('tabAcademe');
}
layer.alert(returnJson.Text);
}, "json")
}
else {
//提示
layer.alert('请填写完整', { title: '提示', icon: 0 });
}
}
- 效果