开始就先写好
///
/// 4.0 修改机场数据
///
/// 实体类
///
public ActionResult ModifyAirport(S_Airport modAirport) {
ReturnJson msg = new ReturnJson();
msg.State = false;
}
在其下面msg.State = false;添加一个//=数据验证
//验证机场三字码 不能为空,有且只有3位
if (!string.IsNullOrEmpty(modAirport.airportCode)&&modAirport.airportCode.Length3)
下面相对应的是:请输入机场三字码,有且只有3位大写字母,在这个的里面有写验证机场名称不能为空if(!string.IsNullOrEmpty(modAirport.airportName))下面对应有机场不能为空,又是一个验证城市名称与拼音码不能为空if (!string.IsNullOrEmpty(modAirport.cityName) && !string.IsNullOrEmpty(modAirport.pinyinName))对应的请输入城市名称和城市拼音,在其中间//判断编号和机场名称是否重复
var airport = from tbAirport in myModel.S_Airport
where tbAirport.airportCodemodAirport.airportCode &&
tbAirport.airportNamemodAirport.airportName
&& tbAirport.airportID!=modAirport.airportID
select tbAirport;
if (airport.Count() == 0) {
//修改数据对象的状态为修改(告诉数据库,我把数据修改了)
myModel.Entry(modAirport).State = System.Data.Entity.EntityState.Modified;
if (myModel.SaveChanges() > 0) {
msg.State = true;
msg.Text = “机场数据修改成功!”;
}else {
msg.Text = “数据修改失败”;
}
}else {
msg.Text = “和其他机场的三字码或者名称重复!”;
}
在跳转到Index增加自定义列模板:修改操作和自定义列模板:删除操作,在新增与修改保存操作的后面一部分添加修改操作$.post("@Url.Content("~/SystemMaintenance/TCCMaintain/ModifyAirport")",
{
airportID: airportID,
airportCode: strAirportCode,
airportName: strAirportName,
cityName: strCityName,
pinyinName: strPinyinName
},
function (msg) {//msg接收控制器方法中返回的对象
if (msg.State == true) {
layer.alert(msg.Text, { icon: 1 });
$("#modalAirport").modal(‘hide’);//关闭模态窗体
TabAirport = layTable.reload(‘tbAirport’);//表格刷新
} else {
layer.alert(msg.Text, {icon:1});
}
});
在最下面写一条删除机场数据function deleteAirportInfor(airportID) {
//alert(airportID);
layer.confirm(“确定要删除吗?”, { icon: 3, title: ‘提示’ }, function (index) {
layer.close(index);
//console.log(“1212”);
$.post("@Url.Content("~/SystemMaintenance/TCCMaintain/DeleteAirport?AirportID=")" + airportID, function (msg) {
if (msg.State) {
layer.alert(msg.Text);
TabAirport = layTable.reload(‘tbAirport’);//表格刷新
}else{
layer.alert(msg.Text,{icon:2});
}
});
});
}
然后在跳转回控制器写出下面这一段
//查询航班表中使用(起飞机场或者目的机场)该机场的数据条数
int count= (from tbFlight in myModel.S_Flight
where tbFlight.orangeID AirportID || tbFlight.destinationID== AirportID
select tbFlight).ToList().Count();
if (count == 0)
{
//查询出要删除的数据
S_Airport modAirport = myModel.S_Airport.Where(m => m.airportID == AirportID).Single();
myModel.S_Airport.Remove(modAirport);
if (myModel.SaveChanges() > 0)
{
msg.State = true;
msg.Text = “机场数据删除成功!”;
}
else {
msg.Text = “机场数据删除失败!”;
}
}
else {
//已经有航班数据
msg.Text = “已有航班数据的城市无法删除”;
}
}
catch (Exception e)
{
Console.WriteLine(e);
msg.Text = "删除数据异常";
}
return Json(msg,JsonRequestBehavior.AllowGet);
}
然后测试一下修改和删除用不用的了,经过测试之后可以使用。