开发工具与关键技术:VS、多表新增
作者:#33
撰写时间:撰写时间:2019年07月06日
在做练习项目中做的多表查询(此处只有两张表):模态窗体是用layui的弹出层做的,新增和修改公用一个模态窗体,申明一个全局变量:var BLWarehouseInfor = true;定义全局变量,true为打开新增模态窗体false为修改。并且新增和修改在一个保存方法里面,只有保存路径(url)不同,其它基本相同,下面是多表的新增:
//------------关闭layer弹出层---------- function layerClose() {layer.close(layerIndex);} //--------------新增仓库模态窗体----------- function insertWarehouse() { BLWarehouseInfor = true; $('#formWarehouse input[type="reset"]').click();//重置表单 layerIndex = layer.open({//弹出layer窗体 type: 1,//(页面层:1) area: ["750px", "490px"], offset: "10px",//默认坐标,即垂直水平居中 title: "新增仓库",//弹出层的标题 content: $("#modalWarehouse")//弹出层模块的ID }); } |
///-----------保存新增、修改导购员信息 --------------- function saveWarehouseInfor() { if ($('#formWarehouse [name="WarehouseName"]').val() != "") { var url = ""; if (BLWarehouseInfor) {url = "/Sales_Process/DataBase/InsertWarehouseInfor"; }else {url = "/Sales_Process/DataBase/UpdateWarehouseInfor";} //序列化表单:新增:名称,修改:ID,名称) var formDate = $("#formWarehouse").serializeArray(); console.log(formDate); $.post(url, formDate, function (msg) { if (msg.State) { layerClose();tabWarehouse = layuiTable.reload('tabWarehouse'); } layer.alert(msg.Text); }); }else { layer.alert('请填写完整', { icon: 0 });} } |
获取from表单的值:
//序列化表单:获取新增和修改的值
var formDate = $("#formWarehouse").serializeArray();
多表新增:基本的新增套路,在单表的基础上再新增一张表,这里两张表:一张主表(仓库表),一张子表(发货表)。先新增子表(ImodSendOutGoods)再新增主表(ImodWarehouse),新增主表时从获取子表的ID(这样才能通过外键了解),并判断是否重复添加仓库(这里只有判断仓库名是否重复),这里通过序列化表单获取数据,就不用一个个写代码获取值。
var formDate = $("#formWarehouse").serializeArray();
public ActionResult InsertWarehouseInfor(S_Warehouse ImodWarehouse, D_SendOutGoods ImodSendOutGoods) { ReturnJson msg = new ReturnJson(); try{if (!string.IsNullOrEmpty(ImodWarehouse.WarehouseName)) { var oldCount = (from tbWarehouse in myModel.S_Warehouse where tbWarehouse.WarehouseName == ImodWarehouse.WarehouseName.Trim() join tbSendOutGoods in myModel.D_SendOutGoods on tbWarehouse.SendOutGoodsID equals tbSendOutGoods.SendOutGoodsID select tbWarehouse).Count(); if (oldCount == 0) { myModel.D_SendOutGoods.Add(ImodSendOutGoods); if (myModel.SaveChanges() > 0) { var SendOutGoodsID = ImodSendOutGoods.SendOutGoodsID; ImodWarehouse.SendOutGoodsID = SendOutGoodsID; myModel.S_Warehouse.Add(ImodWarehouse); if (myModel.SaveChanges() > 0) { msg.State = true; msg.Text = "保存成功!";} else {msg.Text = "保存失败!"; } } else{ msg.Text = "保存失败!";} } else { msg.Text = "仓库已经存在!";} } else { msg.Text = "数据不完整!";} } catch (Exception) {msg.Text = "数据异常!";} return Json(msg, JsonRequestBehavior.AllowGet); } |