Linq单表新增

开发工具与关键技术:Visual Studio 2015 与 jQuery
撰写时间:2020年5月5日

Linq单表新增:
在新增时,核心代码为
myModel.S_Airport.Add(modAirport);
myModel.SaveChange();
新增最繁琐的就是数据验证了
首先获取页面数据
获取三字码
var airportCode=KaTeX parse error: Expected 'EOF', got '#' at position 3: (“#̲frmAirport inpu…(“#frmAirport input[name=’airportName’]”).val();
获取城市名称
var cityName=KaTeX parse error: Expected 'EOF', got '#' at position 3: (“#̲frmAirport inpu…(“#frmAirport input[name=’ PinYinCode]”).val();
获取之后进行条件判断,不允许为空,否则就提示
if (strAirportCode != “” && strAirportCode != null&& strAirportName != “” && strAirportName != null&& strCityName != “” && strCityName != null&& strPinyinName != “” && strPinyinName != null) {
数据传输路径
$.post("/SystemMaintenance/TCCMaintain/InsertAirport"),
{
数据传到键值对的方式进行传递
前者为控制器接收字段:后者为页面获取到的字段信息
airportCode: strAirportCode,
airportName: strAirportName,
cityName: strCityName,
pinyinName: strPinyinName
},提交到控制器中的数据
function (msg) {回调函数
if (msg.State == true) {
$("#modalAirport").modal(‘hide’);关闭模态窗体
layer.alert(msg.Text);弹出新增成功字面提示
TabAirport = layTable.reload(‘tbAirport’);表格刷新
} else {
layer.alert(msg.Text);
}
});
} else {
layer.alert(“表单数据不完整,请检查后再提交数据”);
}
下面是控制代码
就是一大串的数据验证
public ActionResult InsertAirport(S_Airport modAirport) {
new一个实体类用于返回信息
ReturnJson msg = new ReturnJson();
msg.State = false; 设置状态为false,也可不写(bool类型默认为false)
数据验证
验证机场三字码 不能为空,有且只有3位
if (!string.IsNullOrEmpty(modAirport.airportCode) && modAirport.airportCode.Length == 3) {
验证机场名称不能为空
if (!string.IsNullOrEmpty(modAirport.airportName)) {
验证城市名称和城市拼音码不能为空
if (!string.IsNullOrEmpty(modAirport.cityName) && !string.IsNullOrEmpty(modAirport.pinyinName)) {
判断编号和机场名称是否重复
Where 条件查询
var tabAirport = from tbAirport in myModel.S_Airport
where tbAirport.airportCodemodAirport.airportCode||、tbAirport.airportNamemodAirport.airportName
select tbAirport;
if (tabAirport.Count() == 0) {
myModel.S_Airport.Add(modAirport);
如果数据库内数据增加则返回一个true的状态表示成功
if (myModel.SaveChanges() > 0){
msg.State = true;
msg.Text = “新增成功!”;}
else {msg.Text = “新增失败!”;}
}
else {msg.Text = “已经存在相同三字码或者机场名称的机场!”;}
}
else {msg.Text = “城市名称或则拼音码不能为空”;}
}
else {msg.Text = “机场名称不能为空”;}
}
else { msg.Text = “请输入机场三字码,有且只有3位大写字母”;}
return Json(msg,JsonRequestBehavior.AllowGet);
}
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值