예제 #1
0
 public StichtiteForum.Models.Post FormViewEditPost_GetItem([QueryString]int? id)
 {
     var db = new StichtiteForumEntities();
     StichtiteForum.Models.Post post = new Models.Post();
     if (!this.isNew)
     {
         post = db.Posts.Find(this.postId);
     }
     else
     {
         post.CategoryId = db.Categories.FirstOrDefault().CategoryId;
     }
     return post;
 }
예제 #2
0
        public StichtiteForum.Models.Post FormViewEditPost_GetItem([QueryString] int?id)
        {
            var db = new StichtiteForumEntities();

            StichtiteForum.Models.Post post = new Models.Post();
            if (!this.isNew)
            {
                post = db.Posts.Find(this.postId);
            }
            else
            {
                post.CategoryId = db.Categories.FirstOrDefault().CategoryId;
            }
            return(post);
        }
예제 #3
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 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");
            }
        }
예제 #5
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");
            }
        }