Exemplo n.º 1
0
 public ActionResult AddEdit(int? id)
 {
     Excursion excursion = null;
     if (id.HasValue)
     {
         using (var context = new ContentStorage())
         {
             excursion = context.Excursion.Select(c => c).Where(c => c.Id == id).First();
         }
     }
     else
     {
         excursion = new Excursion();
     }
     return View(excursion);
 }
Exemplo n.º 2
0
        public ActionResult AddEdit(FormCollection form, int? Id)
        {
            using (var context = new ContentStorage())
            {
                Excursion excursion;

                if (Id.HasValue && Id.Value > 0)
                {
                    excursion = context.Excursion.Select(e => e).Where(e => e.Id == Id).First();
                }
                else
                {
                    excursion = new Excursion();
                }


                string[] fieldsToUpdate;
                string file = Request.Files["image"].FileName;
                if (!string.IsNullOrEmpty(file))
                {

                    if(!string.IsNullOrEmpty(excursion.ImageSource))
                    {
                        IOHelper.DeleteFile("~/Content/Images", excursion.ImageSource);
                    }

                    string newFileName = IOHelper.GetUniqueFileName("~/Content/Images", file);
                    string filePath = Path.Combine(Server.MapPath("~/Content/Images"), newFileName);
                    Request.Files["image"].SaveAs(filePath);
                    excursion.ImageSource = newFileName;
                    fieldsToUpdate = new string[] { "Title", "ShortDescription", "Text", "Name", "Keywords", "Description", "Price", "SortOrder", "Popular", "ImageSource" };
                }
                else
                {
                    fieldsToUpdate = new string[] { "Title", "ShortDescription", "Text", "Name", "Keywords", "Description", "Price", "SortOrder", "Popular", "ExcursionType" };
                }

                excursion.ExcursionType = Convert.ToInt32(form["excursionType"]);

                TryUpdateModel(excursion, fieldsToUpdate, form.ToValueProvider());


                if (String.IsNullOrEmpty(excursion.Title))
                    ModelState.AddModelError("Title", "Title is required!");
                if (String.IsNullOrEmpty(excursion.ShortDescription))
                    ModelState.AddModelError("ShortDescription", "ShortDescription is required!");
                if (String.IsNullOrEmpty(excursion.Text))
                    ModelState.AddModelError("Text", "Text is required!");
                if (String.IsNullOrEmpty(excursion.Name))
                    ModelState.AddModelError("Name", "Name is required!");


                

                if (ModelState.IsValid)
                {
                    

                    excursion.Text = HttpUtility.HtmlDecode(excursion.Text);
                    excursion.ShortDescription = HttpUtility.HtmlDecode(excursion.ShortDescription);

                    if (excursion.Id == 0)
                        context.AddToExcursion(excursion);

                    context.SaveChanges();
                    return RedirectToAction("Index", "Excursions", new { area = "" });
                }
                return View(excursion);
            }
        }