2012-06-29 8 views
1

asp.net을 사용하여 간단한 블로그 응용 프로그램을 만들려고합니다. 나는 초보자이며 C# 및 ASP .net mvc를 스스로 가르치려고합니다.asp.net에서 주석 db가 업데이트되지 않음

내가 직면하고있는 문제는 게시물에 댓글을 추가하려고 할 때 작동하지 않는다는 것입니다. 게시물에 덧글을 달았지만 아무 것도 표시하지 않습니다. 또한 데이터베이스를 확인할 때 BlogID 필드를 제외하고는 Null 필드 만 포함합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

내 코드는 다음과 같습니다 :

블로그 - 클래스

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

도움이 필요하시면 감사드립니다. 액션 인수로 전달되는 코멘트 인스턴스가 Blog 속성 널 (null)을 가지고 있기 때문에

답변

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

저장하기 전에 블로그와 코멘트를 연결하십시오 :

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 필드를 제외하고는 Null 필드 만 포함합니다. – 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 }); 
    } 
관련 문제