public ActionResult Create(CategoriesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var validImageTypes = new string[]
                {
                    "image/gif",
                    "image/jpeg",
                    "image/pjpeg",
                    "image/png"
                };

                if (model.ImageUpload == null || model.ImageUpload.ContentLength == 0)
                {
                    ModelState.AddModelError("ImageUpload", "This field is required");
                }

                else if (!validImageTypes.Contains(model.ImageUpload.ContentType))
                {
                    ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image.");
                }

                var category = new Category
                {
                    CategoryId          = model.CategoryId,
                    CategoryName        = model.CategoryName,
                    ImageURL            = model.ImageURL,
                    CategoryDescription = model.CategoryDescription
                };

                if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
                {
                    var uploadDir = "/uploads";

                    string finalImageName = "resize-" + model.ImageUpload.FileName.ToString();

                    var imagePath = Path.Combine(Server.MapPath(uploadDir), model.ImageUpload.FileName);
                    var imageUrl  = Path.Combine(uploadDir, finalImageName);

                    //Resize Image
                    ImageResize.ResizeImage(model.ImageUpload);

                    model.ImageUpload.SaveAs(imagePath);
                    category.ImageURL = imageUrl;
                }

                db.Categories.Add(category);
                db.SaveChanges();

                TempData["SuccessMessage"] = "<div class='alert alert-success w-fade-out'><strong> Success!</strong> New Category Created</div>";
                return(RedirectToAction("Index"));
            }

            return(View());
        }
Esempio n. 2
0
        public ActionResult Create([Bind(Include = "Id,ProjectProjectId,CategoryCategoryId")] ProjectCategories projectCategories)
        {
            if (ModelState.IsValid)
            {
                db.ProjectCategories.Add(projectCategories);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ProjectProjectId   = new SelectList(db.Projects, "ProjectId", "ProjectTitle", projectCategories.ProjectProjectId);
            ViewBag.CategoryCategoryId = new SelectList(db.Categories, "CategoryId", "CategoryName", projectCategories.CategoryCategoryId);
            return(View(projectCategories));
        }
Esempio n. 3
0
        public ActionResult Create([Bind(Include = "ProjectId,ProjectTitle,ProjectDescription")] Project project)
        {
            if (ModelState.IsValid)
            {
                db.Projects.Add(project);
                db.SaveChanges();

                TempData["SuccessMessage"] = "<div class='alert alert-success w-fade-out'><strong> Success!</strong> New Project Created</div>";
                return(RedirectToAction("Index"));
            }

            return(View(project));
        }
Esempio n. 4
0
        public void CreateNewSnippet(SnippetsViewModel model)
        {
            Snippet snippet = new Snippet
            {
                SnippetName        = Sanitizer.GetSafeHtmlFragment(model.SnippetName),
                SnippetContent     = model.SnippetContent,
                SnippetDescription = Sanitizer.GetSafeHtmlFragment(model.SnippetDescription),
                ReferenceURL       = Sanitizer.GetSafeHtmlFragment(model.ReferenceUrl),
                SnippetLanguage    = Sanitizer.GetSafeHtmlFragment(model.SnippetLanguage)
            };

            CategorySnippetAssociations snippetCategory = new CategorySnippetAssociations
            {
                SnippetAssociationId  = snippet.SnippetId,
                CategoryAssociationId = model.SelectedCategoryId
            };

            if (model.NoteList != null)
            {
                for (int i = 0; i < model.NoteCount; i++)
                {
                    Note newNote = new Note
                    {
                        NoteSnippetId = snippet.SnippetId,
                        NoteTitle     = model.NoteList[i].NoteTitle,
                        NoteContent   = model.NoteList[i].NoteContent,
                        NoteCount     = model.NoteCount,
                    };

                    db.Notes.Add(newNote);
                }
            }

            db.CategorySnippetAssociations.Add(snippetCategory);
            db.Snippets.Add(snippet);
            db.SaveChanges();
        }