public ActionResult Edit(int?id, string text) { var viewModel = new MedicineLaboratoryViewModel() { Name = text }; if (id.HasValue) { var laboratory = this.db.Laboratories.FirstOrDefault(l => l.Id == id); if (laboratory == null) { return(this.ObjectNotFound()); } viewModel = this.GetViewModel(laboratory); } // the resulting view will depend on the request type return(this.Request.IsAjaxRequest() ? View("EditModal", viewModel) : View("Edit", viewModel)); }
public ActionResult Edit([NotNull] MedicineLaboratoryViewModel formModel) { if (formModel == null) { throw new ArgumentNullException("formModel"); } // if a laboratory exists with the same name, a model state error must be placed var existingLaboratory = this.db.Laboratories.FirstOrDefault(l => l.Name == formModel.Name); if (existingLaboratory != null && existingLaboratory.Id != formModel.Id) { this.ModelState.AddModelError <MedicineLaboratoryViewModel>(model => model.Name, "Já existe um laboratório cadastrado com o mesmo nome"); } if (this.ModelState.IsValid) { Laboratory laboratory = null; if (formModel.Id.HasValue) { laboratory = this.db.Laboratories.FirstOrDefault(l => l.Id == formModel.Id); if (laboratory == null) { return(this.ObjectNotFound()); } } else { laboratory = new Laboratory() { PracticeId = this.DbUser.PracticeId, DoctorId = this.Doctor.Id, CreatedOn = this.GetUtcNow(), }; this.db.Laboratories.AddObject(laboratory); } laboratory.Name = formModel.Name; laboratory.Observations = formModel.Observations; this.db.SaveChanges(); // depending on whether or not this is an Ajax request, // this should return an AutocompleteNewJsonResult or the view if (this.Request.IsAjaxRequest()) { return(this.Json( new AutocompleteNewJsonResult() { Id = laboratory.Id, Value = laboratory.Name })); } // The View here will DEPEND on the caller. // If it's EditModal, it's gonna be EditModal. Otherwise, Edit return(this.RedirectToAction("details", new { id = laboratory.Id })); } // the resulting view will depend on the request type return(this.Request.IsAjaxRequest() ? View("EditModal", formModel) : View("Edit", formModel)); }
public ActionResult Create(MedicineLaboratoryViewModel viewModel) { return(this.Edit(viewModel)); }