public ActionResult Create(Udstillingsmodel udstillingsmodel, HttpPostedFileBase image) { if (ModelState.IsValid) { //upload image if (image != null && image.ContentLength > 0) { try { //Here, I create a custom name for uploaded image string file_name = udstillingsmodel.titel + Path.GetExtension(image.FileName); string path = Path.Combine(Server.MapPath("~/Content/Images"), file_name); image.SaveAs(path); // image_path is nvarchar type db column. We save the name of the file in that column. udstillingsmodel.billedeSti = "/Content/Images" + "/" + file_name; } catch (Exception ex) { } } db.Udstillingsmodels.Add(udstillingsmodel); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(udstillingsmodel)); }
public ActionResult DeleteConfirmed(int id) { Udstillingsmodel udstillingsmodel = db.Udstillingsmodels.Find(id); db.Udstillingsmodels.Remove(udstillingsmodel); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "ID,titel,beskrivelse,billedeSti")] Udstillingsmodel udstillingsmodel) { if (ModelState.IsValid) { db.Entry(udstillingsmodel).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(udstillingsmodel)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Udstillingsmodel udstillingsmodel = db.Udstillingsmodels.Find(id); if (udstillingsmodel == null) { return(HttpNotFound()); } return(View(udstillingsmodel)); }