public EditLineInputModel GetEditLineModel(int?id) { var line = this.Db.Lines .Where(x => x.Id == id) .Include(x => x.Stations) .Include(x => x.Routes) .FirstOrDefault(); if (line == null) { return(null); } var transportTypes = Enum.GetValues(typeof(TypeTransport)).Cast <TypeTransport>() .Select(x => new SelectListItem { Text = Enum.GetName(typeof(TypeTransport), x), Value = x.ToString() }).ToList(); var lineEdit = new EditLineInputModel { Id = line.Id, LineName = line.LineName, TypeTransport = line.Type.ToString(), TransportTypes = transportTypes }; return(lineEdit); }
public IActionResult Edit(EditLineInputModel model) { if (!ModelState.IsValid) { return(this.View(model)); } var result = this.LineService.EditLine(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 EditLine(EditLineInputModel model) { var line = this.Db.Lines.FirstOrDefault(x => x.Id == model.Id); if (line == null) { this.BaseModel.HasError = true; this.BaseModel.Message = MessageConstants.InvalidLineId; return(this.BaseModel); } if (!Enum.TryParse(model.TypeTransport, out TypeTransport typeTransport)) { this.BaseModel.HasError = true; this.BaseModel.Message = MessageConstants.InvalidTypeTransport; return(this.BaseModel); } var hasLineName = this.Db.Lines.Any(x => x.LineName == model.LineName && x.Id != model.Id); if (hasLineName) { this.BaseModel.HasError = true; this.BaseModel.Message = string.Format(MessageConstants.HaveLineName, model.LineName); return(this.BaseModel); } line.LineName = model.LineName; line.Type = typeTransport; try { this.Db.Lines.Update(line); 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); }