public void FormViewEditPost_UpdateItem(int PostId)
        {
            var db = new StichtiteForumEntities();

            if (!this.isNew)
            {
                StichtiteForum.Models.Post post = db.Posts.Find(PostId);
                if (post == null)
                {
                    ModelState.AddModelError("", String.Format(
                        "Post with id {0} was not found", PostId));
                    return;
                }

                int categoryId = Convert.ToInt32(
                    ((DropDownList)FindControlRecursive(this, "DropDownListCategories"))
                    .SelectedValue);
                post.CategoryId = categoryId;

                var fileUploadControl = (FileUpload)this.FormViewEditPost.FindControl("FileUploadControl");
                if (fileUploadControl.HasFile)
                {
                    string filename = 
                        this.GenerateRandomFileName() + '.' + Path.GetExtension(fileUploadControl.FileName);
                    string fullPath = Server.MapPath("~/Uploaded_Files/") + filename;
                    fileUploadControl.SaveAs(fullPath);

                    StichtiteForum.Models.File uploadedFile = new StichtiteForum.Models.File
                    {
                        Path = fullPath,
                        Post = post
                    };

                    post.Files.Add(uploadedFile);
                    db.Files.Add(uploadedFile);
                    db.SaveChanges();
                }

                TryUpdateModel(post);
                if (ModelState.IsValid)
                {
                    db.SaveChanges();
                    Response.Redirect("Default.aspx");
                }
            }
            else
            {
                string title = ((TextBox)FindControlRecursive(this, "TextBoxPostTitle")).Text;
                string content = ((TextBox)FindControlRecursive(this, "TextBoxPostContent")).Text;
                AspNetUser user = db.AspNetUsers.FirstOrDefault(u => u.UserName == this.User.Identity.Name);
                int categoryId = Convert.ToInt32(
                    ((DropDownList)FindControlRecursive(this, "DropDownListCategories"))
                    .SelectedValue);
                
                var post = new StichtiteForum.Models.Post
                {
                    PostDate = DateTime.Now,
                    CategoryId = categoryId,
                    Title = title,
                    Content = content,
                    AspNetUser = user
                };

                var fileUploadControl = (FileUpload)this.FormViewEditPost.FindControl("FileUploadControl");
                if (fileUploadControl.HasFile)
                {
                    string filename = this.GenerateRandomFileName() + '.' + Path.GetExtension(fileUploadControl.FileName);
                    string fullPath = Server.MapPath("~/Uploaded_Files/") + filename;
                    fileUploadControl.SaveAs(fullPath);

                    StichtiteForum.Models.File uploadedFile = new StichtiteForum.Models.File
                    {
                        Path = fullPath,
                        Post = post
                    };

                    post.Files.Add(uploadedFile);
                    db.Files.Add(uploadedFile);
                }

                db.Posts.Add(post);
                db.SaveChanges();
                Response.Redirect("Default.aspx");
            }
        }
Example #2
0
        public void FormViewEditPost_UpdateItem(int PostId)
        {
            var db = new StichtiteForumEntities();

            if (!this.isNew)
            {
                StichtiteForum.Models.Post post = db.Posts.Find(PostId);
                if (post == null)
                {
                    ModelState.AddModelError("", String.Format(
                        "Post with id {0} was not found", PostId));
                    return;
                }

                int categoryId = Convert.ToInt32(
                    ((DropDownList)FindControlRecursive(this, "DropDownListCategories"))
                    .SelectedValue);
                post.CategoryId = categoryId;

                var fileUploadControl = (FileUpload)this.FormViewEditPost.FindControl("FileUploadControl");
                if (fileUploadControl.HasFile)
                {
                    string fileExtension = Path.GetExtension(fileUploadControl.FileName).Substring(1);
                    if (!allowedFileExtensions.Contains(fileExtension.ToLowerInvariant()))
                    {
                        throw new ArgumentException("File type not allowed!");
                    }

                    string filename =
                        this.GenerateRandomFileName() + '.' + fileExtension;
                    string fullPath = Server.MapPath("~/Uploaded_Files/") + filename;

                    fileUploadControl.SaveAs(fullPath);

                    StichtiteForum.Models.File uploadedFile = new StichtiteForum.Models.File
                    {
                        Path = fullPath,
                        Post = post
                    };

                    post.Files.Add(uploadedFile);
                    db.Files.Add(uploadedFile);
                    //try
                    db.SaveChanges();
                }

                TryUpdateModel(post);
                if (ModelState.IsValid)
                {
                    try
                    {
                        db.SaveChanges();
                        ErrorSuccessNotifier.AddSuccessMessage("Post edited succesfully");
                    }
                    catch (Exception ex)
                    {
                        ErrorSuccessNotifier.AddErrorMessage(ex);
                    }

                    Response.Redirect("Default.aspx");
                }
            }
            else
            {
                string title = ((TextBox)FindControlRecursive(this, "TextBoxPostTitle")).Text;
                string content = ((TextBox)FindControlRecursive(this, "TextBoxPostContent")).Text;
                AspNetUser user = db.AspNetUsers.FirstOrDefault(u => u.UserName == this.User.Identity.Name);
                int categoryId = Convert.ToInt32(
                    ((DropDownList)FindControlRecursive(this, "DropDownListCategories"))
                    .SelectedValue);

                var post = new StichtiteForum.Models.Post
                {
                    PostDate = DateTime.Now,
                    CategoryId = categoryId,
                    Title = title,
                    Content = content,
                    AspNetUser = user
                };

                var fileUploadControl = (FileUpload)this.FormViewEditPost.FindControl("FileUploadControl");
                if (fileUploadControl.HasFile)
                {
                    string fileExtension = Path.GetExtension(fileUploadControl.FileName).Substring(1);
                    if (!this.allowedFileExtensions.Contains(fileExtension.ToLower()))
                    {
                        throw new ArgumentException("File type not allowed!");
                    }

                    string filename = this.GenerateRandomFileName() + '.' + fileExtension;
                    string fullPath = Server.MapPath("~/Uploaded_Files/") + filename;
                    fileUploadControl.SaveAs(fullPath);

                    StichtiteForum.Models.File uploadedFile = new StichtiteForum.Models.File
                    {
                        Path = fullPath,
                        Post = post
                    };

                    post.Files.Add(uploadedFile);
                    db.Files.Add(uploadedFile);
                }

                db.Posts.Add(post);
                try
                {
                    db.SaveChanges();
                    ErrorSuccessNotifier.AddSuccessMessage("Post added successfully");

                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }

               // Response.Redirect("Post.aspx?id=" + post.PostId);
                Response.Redirect("Default.aspx");

            }
        }
        public void FormViewEditPost_UpdateItem(int PostId)
        {
            var db = new StichtiteForumEntities();

            if (!this.isNew)
            {
                StichtiteForum.Models.Post post = db.Posts.Find(PostId);
                if (post == null)
                {
                    ModelState.AddModelError("", String.Format(
                                                 "Post with id {0} was not found", PostId));
                    return;
                }

                int categoryId = Convert.ToInt32(
                    ((DropDownList)FindControlRecursive(this, "DropDownListCategories"))
                    .SelectedValue);
                post.CategoryId = categoryId;

                var fileUploadControl = (FileUpload)this.FormViewEditPost.FindControl("FileUploadControl");
                if (fileUploadControl.HasFile)
                {
                    string fileExtension = Path.GetExtension(fileUploadControl.FileName).Substring(1);
                    if (!allowedFileExtensions.Contains(fileExtension.ToLowerInvariant()))
                    {
                        throw new ArgumentException("File type not allowed!");
                    }

                    string filename =
                        this.GenerateRandomFileName() + '.' + fileExtension;
                    string fullPath = Server.MapPath("~/Uploaded_Files/") + filename;

                    fileUploadControl.SaveAs(fullPath);

                    StichtiteForum.Models.File uploadedFile = new StichtiteForum.Models.File
                    {
                        Path = fullPath,
                        Post = post
                    };

                    post.Files.Add(uploadedFile);
                    db.Files.Add(uploadedFile);
                    //try
                    db.SaveChanges();
                }

                TryUpdateModel(post);
                if (ModelState.IsValid)
                {
                    try
                    {
                        db.SaveChanges();
                        ErrorSuccessNotifier.AddSuccessMessage("Post edited succesfully");
                    }
                    catch (Exception ex)
                    {
                        ErrorSuccessNotifier.AddErrorMessage(ex);
                    }

                    Response.Redirect("Default.aspx");
                }
            }
            else
            {
                string     title      = ((TextBox)FindControlRecursive(this, "TextBoxPostTitle")).Text;
                string     content    = ((TextBox)FindControlRecursive(this, "TextBoxPostContent")).Text;
                AspNetUser user       = db.AspNetUsers.FirstOrDefault(u => u.UserName == this.User.Identity.Name);
                int        categoryId = Convert.ToInt32(
                    ((DropDownList)FindControlRecursive(this, "DropDownListCategories"))
                    .SelectedValue);

                var post = new StichtiteForum.Models.Post
                {
                    PostDate   = DateTime.Now,
                    CategoryId = categoryId,
                    Title      = title,
                    Content    = content,
                    AspNetUser = user
                };

                var fileUploadControl = (FileUpload)this.FormViewEditPost.FindControl("FileUploadControl");
                if (fileUploadControl.HasFile)
                {
                    string fileExtension = Path.GetExtension(fileUploadControl.FileName).Substring(1);
                    if (!this.allowedFileExtensions.Contains(fileExtension.ToLower()))
                    {
                        throw new ArgumentException("File type not allowed!");
                    }

                    string filename = this.GenerateRandomFileName() + '.' + fileExtension;
                    string fullPath = Server.MapPath("~/Uploaded_Files/") + filename;
                    fileUploadControl.SaveAs(fullPath);

                    StichtiteForum.Models.File uploadedFile = new StichtiteForum.Models.File
                    {
                        Path = fullPath,
                        Post = post
                    };

                    post.Files.Add(uploadedFile);
                    db.Files.Add(uploadedFile);
                }

                db.Posts.Add(post);
                try
                {
                    db.SaveChanges();
                    ErrorSuccessNotifier.AddSuccessMessage("Post added successfully");
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }

                // Response.Redirect("Post.aspx?id=" + post.PostId);
                Response.Redirect("Default.aspx");
            }
        }