2012-06-29 44 views
1

我想創建一個簡單的博客應用程序使用asp.net。我是一個新手,並試圖教自己c#和asp.net.net mvc。評論db沒有得到更新的asp.netnet

我面臨的問題是,當我嘗試添加評論到帖子然後它不起作用。我確實收到了關於該帖子的評論,但沒有顯示任何內容。此外,當我檢查數據庫 - 它只包含空字段,除了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; } 
    } 

BlogDetailViewModel

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 }); 
    } 

任何幫助將不勝感激。

回答

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 }); 
} 
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 }); 
} 
+0

無法正常工作......我仍然收到附加到帖子的評論,但它沒有顯示任何內容。此外,當我檢查數據庫 - 它只包含空字段,除了BlogID字段。 – Tripping

+0

我認爲問題在於,asp .net mvc3無法將註釋控制器的創建操作中的數據與(註釋c)綁定在一起。我如何解決這個問題? – Tripping

+0

@Tripping將參數'c'的名稱更改爲'comment'。它與您在viewmodel中定義的名稱相同。 – ZippyV

0

我想你應該添加屬性HttpPost到創建的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 }); 
    }