public ActionResult Create(PlaceCreator model, HttpPostedFileBase[] files) { int res = 0; if (ModelState.IsValid) { if (files.Count() > 5) { return Create("Не можна додавати більше 5 фото"); } using (var db = new RazomContext()) { Places p = new Places() { CityID = model.SelectedCity, Name = model.Place.Name, PlaceTypeID = model.SelectedPlaceType, Address = model.Place.Address, }; db.Places.Add(p); db.SaveChanges(); res = p.PlaceID; foreach (var file in files) { byte[] image = new byte[file.ContentLength]; using (BinaryReader r = new BinaryReader(file.InputStream)) { image = r.ReadBytes(file.ContentLength); } db.Database.ExecuteSqlCommand("INSERT INTO PhotosPlace(PlaceID,FileFoto) Values({0},{1})", p.PlaceID, image); } } return RedirectToAction("Show", "Place", new { id = res }); } return Create("Не введена назва або адреса"); }
public ActionResult Create(string message="") { if (message != "") { ModelState.AddModelError(String.Empty,message); } PlaceCreator p = new PlaceCreator(); p.Place = new FullPlace(); using(var db = new RazomContext()) { p.PlaceTypes = new SelectList(db.PlaceType.OrderBy(m => m.Type).ToList(),"PlaceTypeID","Type"); p.Cities = new SelectList(db.Region.OrderBy(m => m.Name).Where(m => m.Name != "Невідомо").ToList(), "CityID", "Name"); } return View(p); }