public IActionResult Edit(EditStationInputModel model) { if (!ModelState.IsValid) { return(this.View(model)); } var result = this.StationService.EditStation(model); if (!result.HasError) { this.TempData.Put(MessageConstants.Name, new MessageModel { Type = MessageType.Success, Message = result.Message }); } else { this.TempData.Put(MessageConstants.Name, new MessageModel { Type = MessageType.Danger, Message = result.Message }); return(this.RedirectToAction(nameof(Edit), new { id = model.Id })); } return(this.RedirectToAction(nameof(Index))); }
public BaseModel EditStation(EditStationInputModel model) { var station = this.Db.Stations.FirstOrDefault(x => x.Id == model.Id); if (station == null) { this.BaseModel.HasError = true; this.BaseModel.Message = MessageConstants.InvalidStationId; return(this.BaseModel); } var hasStationName = this.Db.Stations.Any(x => x.StationName == model.StationName && x.Id != model.Id); var hasStationCode = this.Db.Stations.Any(x => x.StationCode == model.StationCode && x.Id != model.Id); if (hasStationName) { this.BaseModel.HasError = true; this.BaseModel.Message = string.Format(MessageConstants.HaveStationName, model.StationName); return(this.BaseModel); } if (hasStationCode) { this.BaseModel.HasError = true; this.BaseModel.Message = string.Format(MessageConstants.HaveStationCode, model.StationCode); return(this.BaseModel); } station.StationName = model.StationName; station.StationCode = model.StationCode; try { this.Db.Stations.Update(station); this.Db.SaveChanges(); this.BaseModel.HasError = false; this.BaseModel.Message = MessageConstants.EditStation; } catch (Exception) { this.BaseModel.HasError = true; this.BaseModel.Message = MessageConstants.NoEditStation; } return(this.BaseModel); }