单表新增数据

一、
定义一个新增的模态框的方法调用

function openInsertModal() {

//设置新增的标识

isInsert = true;

//重置表单

$(’#frmAirport
[type=“reset”]’).click();

//设置model标题

$("#modalTitle").text(“新增三字码”);

//显示模态框

$("#modalAirport").modal(‘show’);

}

二、 点击确认按钮,保存数据

function saveAirportInfor() {

//==获取参数

 var airportCode = $('#frmAirport

[name=“airportCode”]’).val();//三字码

 var airportName = $('#frmAirport

[name=“airportName”]’).val();//机场名称

 var cityName = $('#frmAirport [name="cityName"]').val();//城市名称

 var pinyinName = $('#frmAirport [name="pinyinName"]').val();//城市拼音名称

//==-数据验证

 if(airportCode!=undefined

&& airportCode!="" &&

airportName!=undefined
&& airportName!="" &&

  cityName!=undefined && cityName!="" &&

  pinyinName != undefined &&

pinyinName != “”){

  //==发送请求到控制器

  if (isInsert) {

  //===新增的保存

  //打开加载层

  var layerIndex = layer.load();

  //发送请求

$.post("@Url.Content("~/SystemMaintenance/TCCMaintain/InserAirport")",{

  airportCode: airportCode, //三字码

  airportName: airportName,//机场名称

  cityName:

cityName,//城市名称

pinyinName:
pinyinName//城市拼音名称

},

  function (msg) {//关闭加载层

  layer.close(layerIndex);

if (msg.State) {//关闭模态框

$("#modalAirport").modal(‘hide’);

//刷新table

tabAirport.reload({

page: {

curr: 1 //重新从第 1 页开始

} });

layer.alert(msg.Text,
{ icon: 1 });

} else {

layer.alert(msg.Text,
{ icon: 2 });

} }); } }

三、 在控制器里面验证数据是否存在

public ActionResult InserAirport(S_Airport airport){

ReturnJson msg = new ReturnJson();

//1==-验证数据

//1.1==-验证三字码

if
(airport.airportCode!=null && airport.airportCode.Length==3){

//1.2==-机场名称

if (!string.IsNullOrEmpty(airport.airportName)){

//1.3==-城市名称

if (!string.IsNullOrEmpty(airport.cityName)){

//1.4==-拼音码

if (!string.IsNullOrEmpty(airport.pinyinName)){

//2==-判断要新增的三字码 or 机场名称是否已经存在

int oldAirportCount = (from tabAirport in myModels.S_Airport

where
tabAirport.airportCode == airport.airportCode

|| tabAirport.airportName
== airport.airportName

select
tabAirport).Count();

//2.1==-没有重复就新增

if (oldAirportCount ==
0){

//3==-新增数据到数据库

//3.1==-add需要新增的数据实例

myModels.S_Airport.Add(airport);

//3.2==-保存新增到数据库

if
(myModels.SaveChanges() > 0){

msg.State = true;

msg.Text = “新增成功”;}else{

msg.Text = “新增失败”;

} }else{

msg.Text = “已经存在该三字码或者机场名称,不能添加”;

} }else{

msg.Text = “城市拼音码名称不能为空”;

} }else{

msg.Text = “请填写城市名称”;

} }else{

msg.Text = “请填写机场名称”;

} }else{

msg.Text = “三字码只能有3位”;}

return Json(msg, JsonRequestBehavior.AllowGet);}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值