简体   繁体   中英

Adding comment to post in ASP.NET MVC

EDIT: I am trying to add a comment to a post in my mvc application but my action does not seem to work. When I reach the Action I want to pass on an Id from a Post (the Id from the table/model FormalBlog) but the newPost.Post = model.Post is null and when the action reaches the db.SaveChanges it throws an System.Data.Entity.Validation.DbEntityValidationException.

Below is my action and my PostIndexViewModel:

     public ActionResult Comment(PostIndexViewModel model, FormalBlog Post)
    {
        var userName = User.Identity.Name;
        var author = db.Users.SingleOrDefault(x => x.UserName == userName);

        Comment newPost = new Comment();

        newPost.Author = author;
        newPost.Text = model.Text;
        newPost.Post = model.Post;


        db.Comments.Add(newPost);
        db.SaveChanges();
        return RedirectToAction("ShowBlogs", "Blog");

    }
}

public class PostIndexViewModel
{
    public string Id { get; set; }
    public ICollection<FormalBlog> FormalBlogs { get; set; }
    public FormalBlog NewFormalBlog { get; set; } = new FormalBlog();
    public Category NewCategory { get; set; } = new Category();
    public ICollection<Category> Categories { get; set; }
    public List<SelectListItem> SelectedCategories { get; set; }
    public int[] CategoryIds { get; set; }
    public Category CategoryN { get; set; }
    public ICollection<Meeting> Meetings { get; set; } //testrad

   // public int Id { get; set; }
    public string Text { get; set; }
    public ApplicationUser Author { get; set; }
    public Comment NewComment { get; set; }
    public FormalBlog Post { get; set; }

}

and here is the code for my view:

@model XP_Scrum_Grupp2.Controllers.PostIndexViewModel

@using (Html.BeginForm("Comment", "Blog", new { formal = Model }, FormMethod.Post, new { id = Model.Id }))
{

    <div class="comment-form-container">
        <form class="comment-form" data-action="@Url.Action("Comment", "Blog")">
            @Html.HiddenFor(m => m.Id)

            <div>@Html.DisplayFor(m => m.Author)</div>
            <div>
                <div>@Html.LabelFor(m => m.Text)</div>
                @Html.TextAreaFor(m => m.Text, new { Class = "comment-text", rows = "3", cols = "50" })
            </div>
            <div class="comment-result" style="display: none;">
                <span class="comment-result-text">An error occurred</span>
            </div>
            <div>
                <button type="submit" class="comment-form-submit">Submit comment</button>
            </div>
        </form>
    </div>
}

You are not posting the any data as model.NewComment.Text so the error occures because of NewComment object is null .

@Html.TextAreaFor(m => m.Text, new { Class = "comment-text", rows = "3", cols = "50" })

So, try to change it;

newPost.Text = model.NewComment.Text;

to

newPost.Text = model.Text;

Your view does not assign any value to model.NewComment . So when you access model.NewComment.Text it will throw Null reference exception since model.NewComment is null.

You assign new text to model's Test property. So you should use model.Text instead of model.NewComment.Text

public ActionResult Comment(PostIndexViewModel model)
    {
        var userName = User.Identity.Name;
        var author = db.Users.SingleOrDefault(x => x.UserName == userName);

        Comment newPost = new Comment();

        newPost.Author = author;
        newPost.Text = model.Text;
        newPost.Post = model.Post;


        db.Comments.Add(newPost);
        db.SaveChanges();
        return RedirectToAction("ShowBlogs", "Blog");

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM