一、打开修改模态框
function
openUpdate(airportID) {
//设置修改的标识
isInsert = false;
//重置表单
$(’#frmAirport
[type=“reset”]’).click();
//设置model标题
$("#modalTitle").text(“修改三字码”);
//====回填被修改的机场数据
$.post("@Url.Content("~/SystemMaintenance/TCCMaintain/SelectAirportByID")",{
airportID:
airportID,
},
function (jsonData) {
if (jsonData != null) {
//回填数据
$(’#frmAirport
[name=“airportID”]’).val(jsonData.airportID);//机场ID
!!作为修改数据时的条件
$(’#frmAirport
[name=“airportCode”]’).val(jsonData.airportCode.trim());//三字码
$(’#frmAirport
[name=“airportName”]’).val(jsonData.airportName.trim());//机场名称
$(’#frmAirport
[name=“cityName”]’).val(jsonData.cityName.trim());//城市名称
$(’#frmAirport
[name=“pinyinName”]’).val(jsonData.pinyinName.trim());//城市拼音名称
//显示模态框
$("#modalAirport").modal(‘show’);
} else {
layer.alert(“参数异常”);
} }); }
二、回填数据传递到控制器
//获取修改机场的主键ID
var airportID = $(’#frmAirport
[name=“airportID”]’).val();//机场ID
if (airportID !=
undefined && airportID != “” && airportID > 0) {
//===修改的保存
//打开加载层
var layerIndex =
layer.load();
//发送请求
$.post("@Url.Content("~/SystemMaintenance/TCCMaintain/UpdateAirport")",{
airportID:airportID,//机场ID
airportCode:
airportCode, //三字码
airportName:
airportName,//机场名称
cityName: cityName,//城市名称
pinyinName:
pinyinName//城市拼音名称
},
function (msg) {
//关闭加载层
layer.close(layerIndex);
if (msg.State) {
//关闭模态框
$("#modalAirport").modal(‘hide’);
//刷新table
tabAirport.reload();
layer.alert(msg.Text,
{ icon: 1 });
} else {
layer.alert(msg.Text,
{ icon: 2 });}
});
} else {
layer.alert(“机场信息异常,请稍后再试”);
} }
三、控制器修改数据
1、根据主键ID查询机场信息
public ActionResult SelectAirportByID(int airportID){
try{
S_Airport dbairport = (from tabAirort in myModels.S_Airport
where tabAirort.airportID
== airportID
select tabAirort).Single();
return Json(dbairport, JsonRequestBehavior.AllowGet);}
catch (Exception e){
Console.WriteLine(e);
return null;
} }
2、修改信息
public ActionResult UpdateAirport(S_Airport airport){
ReturnJson msg = new ReturnJson();
//===1-验证数据
//==1.0-验证被修改机场的ID必须大于0
if (airport.airportID
0){
//==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.3-城市拼音名称
if (!string.IsNullOrEmpty(airport.pinyinName)){
//===2-判断要修改的机场数据的三字码 or 机场名称和其他机场是否有相同的
int
oldOtherAirportCount = (from tabAirport in myModels.S_Airport
where
tabAirport.airportID != airport.airportID
&& (tabAirport.airportCode
== airport.airportCode
||
tabAirport.airportName == airport.airportName)
select
tabAirport).Count();
if
(oldOtherAirportCount == 0){
//===3- 保存修改后的数据到数据库
//3.1-标记该条数据被修改
myModels.Entry(airport).State
= System.Data.Entity.EntityState.Modified;
//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位”;
} }
else{
msg.Text = “参数异常”;
}
return Json(msg, JsonRequestBehavior.AllowGet);}