Esempio n. 1
0
        public Noticeboard.Models.Post FormViewEditPost_GetItem([QueryString] int?id)
        {
            var db = new NoticeboardEntities();

            Noticeboard.Models.Post post = new Models.Post();
            if (!this.isNew)
            {
                post = db.Posts.Find(this.postId);
            }
            else
            {
                post.CategoryId = db.Categories.FirstOrDefault().CategoryId;
            }
            return(post);
        }
Esempio n. 2
0
        public void FormViewEditPost_UpdateItem(int PostId)
        {
            var db = new NoticeboardEntities();

            if (!this.isNew)
            {
                Noticeboard.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);

                    Noticeboard.Models.File uploadedFile = new Noticeboard.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 Noticeboard.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);

                    Noticeboard.Models.File uploadedFile = new Noticeboard.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");
            }
        }