1

我正在尝试使用 asp.net 创建一个简单的博客应用程序。我是一个新手,正在尝试自学 c# 和 asp .net mvc。

我面临的问题是,当我尝试向帖子添加评论时,它不起作用。我确实收到了附加到帖子的评论,但它没有显示任何内容。另外,当我检查数据库时,它只包含 Null 字段,但 BlogID 字段除外。我究竟做错了什么?

我的代码如下:

博客 - 类

public class Blog
{
    public int BlogID { get; set; }
    public string Title { get; set; }
    public string Writer { get; set; }

    [DataType(DataType.MultilineText)]
    public string Excerpt { get; set; }

    [DataType(DataType.MultilineText)]
    public string Content { get; set; }

    [DataType(DataType.Date)]
    public DateTime PublishDate { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
}

评论 - 类

public class Comment
    {
        public int CommentID { get; set; }
        public string Name { get; set; }

        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [DataType(DataType.MultilineText)]
        public string CommentBody { get; set; }

        public virtual int BlogID { get; set; } 
        public virtual Blog Blog { get; set; }
    }

博客详细视图模型

public class BlogDetailsViewModel
{
    public Blog Blog { get; set; }
    public Comment Comment { get; set; }
}

博客 - 详细信息控制器

public ViewResult Details(int id)
{

    Blog blog = db.Blogs.Find(id);
    BlogDetailsViewModel viewModel = new BlogDetailsViewModel {Blog = blog};
    return View(viewModel);
}

博客详细信息 - 查看

@model NPLHBlog.ViewModels.BlogDetailsViewModel

有代码在下面的博客详细信息页面末尾显示博客帖子 + 评论表单:

   @using (Html.BeginForm("Create", "Comment"))
    {
        <input type="hidden" name="BlogID" value="@Model.Blog.BlogID" />
        <div class="editor-label">
            @Html.LabelFor(model => model.Comment.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Comment.Name)
            @Html.ValidationMessageFor(model => model.Comment.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Comment.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Comment.Email)
            @Html.ValidationMessageFor(model => model.Comment.Email)
        </div>            

        <div class="editor-label">
            @Html.LabelFor(model => model.Comment.CommentBody)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Comment.CommentBody)
            @Html.ValidationMessageFor(model => model.Comment.CommentBody)
        </div>
        <p>
            <input type="submit" value="Add Comment" />
        </p>

评论 - 创建控制器

public class CommentController : Controller
{
    private NPLHBlogDb db = new NPLHBlogDb();

    //
    // GET: /Comment/

    public ActionResult Create(Comment c)
    {
        Blog blog = db.Blogs.Single(b => b.BlogID == c.BlogID);
        blog.Comments.Add(c);
        db.SaveChanges();
        return RedirectToAction("Details", "Blog", new { ID = c.BlogID });
    }

任何帮助将不胜感激。

4

3 回答 3

1

找到了答案...我将评论控制器的创建操作更新为:

public ActionResult Create(BlogDetailsViewModel viewModel)
{
    Blog blog = db.Blogs.Single(b => b.BlogID == viewModel.Comment.BlogID);
    Comment c = viewModel.Comment;
    blog.Comments.Add(c);
    db.SaveChanges();
    return RedirectToAction("Details", "Blog", new { ID = c.BlogID });
}
于 2012-06-29T10:41:49.860 回答
0

在保存之前尝试将评论与博客相关联,因为作为操作参数传递的评论实例的Blog属性为空:

public ActionResult Create(Comment c)
{
    Blog blog = db.Blogs.Single(b => b.BlogID == c.BlogID);
    blog.Comments.Add(c);

    // associate the comment with the blog that you have retrieved
    c.Blog = blog;

    db.SaveChanges();
    return RedirectToAction("Details", "Blog", new { ID = c.BlogID });
}
于 2012-06-29T10:18:08.723 回答
0

我认为您应该将HttpPost属性添加到 Create ActionResult 方法。

[HttpPost]
public ActionResult Create(Comment c)
    {
        Blog blog = db.Blogs.Single(b => b.BlogID == c.BlogID);
        blog.Comments.Add(c);
        db.SaveChanges();
        return RedirectToAction("Details", "Blog", new { ID = c.BlogID });
    }
于 2012-06-29T10:44:20.387 回答