예제 #1
0
        public ActionResult Create(Content content, IEnumerable<HttpPostedFileBase> images, HttpPostedFileBase pdf, HttpPostedFileBase featured)
        {
            if (ModelState.IsValid)
            {
                content.Timestamp = DateTime.Now;
                content.TimestampEdit = DateTime.Now;
                db.Content.AddObject(content);
                db.SaveChanges();

                bool IsExist = Directory.Exists(Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id));
                if (!IsExist)
                    Directory.CreateDirectory(Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id));

                if (featured != null)
                {
                    var filename = Guid.NewGuid();
                    var path = Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id) + @"\" + filename.ToString().Substring(0, 8) + ".jpg";
                    featured.SaveAs(path);
                    ContentImages __newimage = new ContentImages() { FileGUID = filename, IdContent = content.Id, URL = "/Content/Upload/" + content.IdContentType + "/" + content.Id + "/" + filename.ToString().Substring(0, 8) + ".jpg", IsDefault = true };
                    db.ContentImages.AddObject(__newimage);
                    db.SaveChanges();
                }

                foreach (var item in images)
                {
                    if (item != null)
                    {
                        var filename = Guid.NewGuid();
                        var path = Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id) + @"\" + filename.ToString().Substring(0, 8) + ".jpg";
                        item.SaveAs(path);
                        ContentImages __newimage = new ContentImages() { FileGUID = filename, IdContent = content.Id, URL = "/Content/Upload/" + content.IdContentType + "/" + content.Id + "/" + filename.ToString().Substring(0, 8) + ".jpg", IsDefault = false };
                        db.ContentImages.AddObject(__newimage);
                        db.SaveChanges();
                    }
                }

                if (pdf != null)
                {
                    var filename = Guid.NewGuid();
                    var path = Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id) + @"\" + filename.ToString().Substring(0, 8) + ".pdf";
                    pdf.SaveAs(path);
                    ContentFiles __newPDF = new ContentFiles() { FileGUID = filename, IdContent = content.Id, URL = "/Content/Upload/" + content.IdContentType + "/" + content.Id + "/" + filename.ToString().Substring(0, 8) + ".pdf", IsDefault = true };
                    db.ContentFiles.AddObject(__newPDF);
                    db.SaveChanges();
                }

                return RedirectToAction("Index", new { Id = content.IdContentType });
            }

            ViewBag.IdContentType = new SelectList(db.ContentType, "Id", "Name", content.IdContentType);
            ViewBag.IdCountry = new SelectList(db.Country, "Id", "Country1", content.IdCountry);
            return View(content);
        }
예제 #2
0
 /// <summary>
 /// Create a new ContentFiles object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="fileGUID">Initial value of the FileGUID property.</param>
 /// <param name="uRL">Initial value of the URL property.</param>
 /// <param name="isDefault">Initial value of the IsDefault property.</param>
 /// <param name="idContent">Initial value of the IdContent property.</param>
 public static ContentFiles CreateContentFiles(global::System.Int32 id, global::System.Guid fileGUID, global::System.String uRL, global::System.Boolean isDefault, global::System.Int32 idContent)
 {
     ContentFiles contentFiles = new ContentFiles();
     contentFiles.Id = id;
     contentFiles.FileGUID = fileGUID;
     contentFiles.URL = uRL;
     contentFiles.IsDefault = isDefault;
     contentFiles.IdContent = idContent;
     return contentFiles;
 }
예제 #3
0
 /// <summary>
 /// Deprecated Method for adding a new object to the ContentFiles EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToContentFiles(ContentFiles contentFiles)
 {
     base.AddObject("ContentFiles", contentFiles);
 }
예제 #4
0
        public ActionResult Edit(Content content, IEnumerable<HttpPostedFileBase> images, HttpPostedFileBase pdf, HttpPostedFileBase featured, ContentMedia MetaData)
        {
            if (ModelState.IsValid)
            {

                content.TimestampEdit = DateTime.Now;

                db.Content.Attach(content);
                db.ObjectStateManager.ChangeObjectState(content, EntityState.Modified);
                db.SaveChanges();

                if (featured != null)
                {
                    var filename = Guid.NewGuid();
                    var path = Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id) + @"\" + filename.ToString().Substring(0, 8) + ".jpg";
                    featured.SaveAs(path);
                    ContentImages __newimage = new ContentImages() { FileGUID = filename, IdContent = content.Id, URL = "/Content/Upload/" + content.IdContentType + "/" + content.Id + "/" + filename.ToString().Substring(0, 8) + ".jpg", IsDefault = true };

                    var __todelete = db.ContentImages.Where(u => u.URL.Contains(MetaData.image)).FirstOrDefault();
                    if (__todelete != null)
                    {
                        db.ContentImages.DeleteObject(__todelete);
                        db.SaveChanges();
                    }

                    db.ContentImages.AddObject(__newimage);
                    db.SaveChanges();
                }
                int i = 1;

                foreach (var item in images)
                {
                    if (item != null)
                    {
                        var filename = Guid.NewGuid();
                        var path = Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id) + @"\" + filename.ToString().Substring(0, 8) + ".jpg";
                        item.SaveAs(path);
                        ContentImages __newimage = new ContentImages() { FileGUID = filename, IdContent = content.Id, URL = "/Content/Upload/" + content.IdContentType + "/" + content.Id + "/" + filename.ToString().Substring(0, 8) + ".jpg", IsDefault = false };

                        string namefile = string.Empty;

                        switch (i)
                        {

                            case 1:
                                namefile = MetaData.img1;
                                break;
                            case 2:
                                namefile = MetaData.img2;
                                break;
                            case 3:
                                namefile = MetaData.img3;
                                break;
                            case 4:
                                namefile = MetaData.img4;
                                break;
                            default:
                                namefile = MetaData.img1;
                                break;
                        }

                        var __todelete = db.ContentImages.Where(u => u.URL.Contains(namefile)).FirstOrDefault();
                        if (__todelete != null)
                        {
                            db.ContentImages.DeleteObject(__todelete);
                            db.SaveChanges();
                        }

                        db.ContentImages.AddObject(__newimage);
                        db.SaveChanges();
                    }
                    i = i + 1;
                }

                if (pdf != null)
                {
                    var filename = Guid.NewGuid();
                    var path = Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id) + @"\" + filename.ToString().Substring(0, 8) + ".pdf";
                    pdf.SaveAs(path);
                    ContentFiles __newPDF = new ContentFiles() { FileGUID = filename, IdContent = content.Id, URL = "/Content/Upload/" + content.IdContentType + "/" + content.Id + "/" + filename.ToString().Substring(0, 8) + ".pdf", IsDefault = true };

                    var __todelete = db.ContentFiles.Where(u => u.URL.Contains(MetaData.pdf)).FirstOrDefault();
                    if (__todelete != null)
                    {
                        db.ContentFiles.DeleteObject(__todelete);
                        db.SaveChanges();
                    }

                    db.ContentFiles.AddObject(__newPDF);
                    db.SaveChanges();
                }

                return RedirectToAction("Index", new { Id = content.IdContentType });
            }

            ViewBag.IdContentType = new SelectList(db.ContentType, "Id", "Name", content.IdContentType);
            ViewBag.IdCountry = new SelectList(db.Country, "Id", "Country1", content.IdCountry);
            return View(content);
        }