一、
定义一个新增的模态框的方法调用
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);}