public ActionResult CreateStay(StaysViewModel vm) { if (!ModelState.IsValid) { var viewModel = new StaysViewModel { Stays = new Stay(), Cities = _context.Cities.ToList(), PropertyTypes = _context.PropertyTypes.ToList() }; } if (vm.Stays.Id == 0) { _context.Stays.Add(vm.Stays); } else { var staysInDb = _context.Stays.FirstOrDefault(c => c.Id == vm.Stays.Id); staysInDb.StayName = vm.Stays.StayName; staysInDb.Address = vm.Stays.Address; staysInDb.Price = vm.Stays.Price; staysInDb.CityId = vm.Stays.CityId; staysInDb.PropertyTypeId = vm.Stays.PropertyTypeId; staysInDb.Description = vm.Stays.Description; } _context.SaveChanges(); return(RedirectToAction("Index", "Stays")); }
//Country autofill with ADO.NET //public JsonResult CountryList(int Id) //{ // string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; // SqlConnection connection = new SqlConnection(connectionString); // string query = "SELECT country.Id, country.CountryName FROM Cities AS city INNER JOIN Countries AS country ON city.CountryId = country.Id WHERE city.Id = '" + Id + "'"; // SqlCommand cm = new SqlCommand(query, connection); // connection.Open(); // SqlDataReader dataReader = cm.ExecuteReader(); // List<SelectListItem> list = new List<SelectListItem>(); // while (dataReader.Read()) // { // list.Add(new SelectListItem { Text = dataReader[1].ToString(), Value = dataReader[0].ToString() }); // } // connection.Close(); // return Json(list, JsonRequestBehavior.AllowGet); //} //LINQ Autofill Country public JsonResult CountryList(int id) { var cities = _context.Cities.Include(c => c.Country) .Where(c => c.CountryId == c.Country.Id) .Where(c => c.Id == id) .ToList(); var viewModel = new StaysViewModel { Stays = new Stay(), Cities = cities, PropertyTypes = _context.PropertyTypes.ToList() }; List <SelectListItem> list = new List <SelectListItem>(); cities.ForEach(x => { list.Add(new SelectListItem { Text = x.Country.CountryName, Value = x.CountryId.ToString() }); }); return(Json(list, JsonRequestBehavior.AllowGet)); }
//CREATE: view public ActionResult CreateStay() { var types = _context.PropertyTypes.ToList(); var cities = _context.Cities.ToList(); var viewModel = new StaysViewModel { Stays = new Stay(), Cities = cities, PropertyTypes = types, }; return(View(viewModel)); }
//EDIT public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var stays = _context.Stays.Find(id); var viewModel = new StaysViewModel { Stays = stays, Cities = _context.Cities.ToList(), PropertyTypes = _context.PropertyTypes.ToList() }; if (stays == null) { return(HttpNotFound()); } return(View("CreateStay", viewModel)); }
public ActionResult Delete(int id) { try { Stay stays = _context.Stays.Find(id); var viewModel = new StaysViewModel { Stays = stays, Cities = _context.Cities.ToList(), PropertyTypes = _context.PropertyTypes.ToList() }; _context.Stays.Remove(stays); _context.SaveChanges(); } catch { return(RedirectToAction("DeleteStay", new { saveChangesError = true })); } return(RedirectToAction("Index", "Stays")); }