Пример #1
0
        public ActionResult Create([Bind(Include = "Producer_Id,Producer_Name,Producer_Photo,Producer_Sex,Producer_DOB,Producer_Bio,Producer_Photo_Data")] ProducerModel ProducerModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (ProducerModel.Producer_Photo_Data == null)
                    {
                        ViewBag.FileStatus = "Please select image.";
                        return(View(ProducerModel));
                    }
                    else
                    {
                        ProducerModel.Producer_Photo = ProducerModel.Producer_Photo_Data.FileName;
                        String path     = Server.MapPath("Image");
                        String filename = Path.GetFileName(ProducerModel.Producer_Photo_Data.FileName);
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        else
                        {
                            String fullpath = Path.Combine(path, filename);
                            ProducerModel.Producer_Photo_Data.SaveAs(fullpath);
                        }
                    }
                    _ProducerRep.AddProducer(ProducerModel);

                    _ProducerRep.save();
                    return(RedirectToAction("Index"));
                }
            }
            catch (DataException e)
            {
                ViewBag.FileStatus = e;
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return(View(ProducerModel));
        }