【问题标题】:insert a related data in MVC4在MVC4中插入相关数据
【发布时间】:2013-08-10 13:32:03
【问题描述】:

我有这两个模型类:

 public class Article
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public ICollection<Comment> Comments { get; set; }
    }
    public class Comment
    {
        public int ID { get; set; }
        public int ArticleID { get; set; }
        public string CommentTxt { get; set; }
        public Article Article { get; set; }
    }
    public class ArticleDbContext : DbContext
    {
        public DbSet<Article> Articles { get; set; }
        public DbSet<Comment> Comments { get; set; }
    }

现在我想要一个页面,列出为一篇文章插入的所有评论,并且在列表下方我可以为那篇文章插入新评论?

【问题讨论】:

  • 到目前为止您尝试过什么?如果您对控制器和视图一无所知,我推荐这个免费视频系列。深入和很好的解释asp.net/mvc/videos/…
  • 你需要一个 ViewModel 来处理这种东西,见here

标签: c# asp.net-mvc-4 ef-code-first


【解决方案1】:

您可以为此创建视图模型:

public class ArticleViewModel
{ 
   public Article Article { get; set; }
   public List<Comment> Comments { get; set; }
}

在控制器中:

public ActionResult Details(int id)
{
   ArticleViewModel model = new ArticleViewModel();

   model.Article = _yourDBRepository.GetArticleById(id);
   model.Comments = _yourDBRepository.LoadCommentsByArticleId(id);
   return View(model);
}

在视图中,您可以像这样添加新的评论项:

  @using (Html.BeginForm("Create", "Comment", FormMethod.Post))
    {
      @Html.Hidden("ArticleId", Model.Article.Id)
      @Html.TextBox("text", "", new { name = "text", id = "text", data_placeholder = "Enter comment" })
    }

【讨论】:

    【解决方案2】:

    在控制器中

    //** Get Article
    Public ActionResult Article (int ID)
    {
    Article article = db.Find(ID);
    return(article)
    }
    
    //*Add comments
    [HttpPost]
    public ActionResult Addcomment (int articleID, string CommentTxt)
    {
      Article article = db.Find(ID);
      Comment comment = new Comment();
      db.Comments.Add(comment);
      comment.ArticleID = articleID;
      comment.CommentTxt = CommentTxt;
      db.SaveChanges;
    
      return PartialView(article);
    }
    
    
    //**Get List of comments after adding new comment
    public ActionResult AllComments(int articleID)
    {
      Article article = db.Find(ID);
      var comments = db.Comments.Where(a=>a.ArticleID == articleID).ToList();
      return PartialView(comments);
    }
    

    在您的文章视图中:

    <sript>
    $('.addComment')click(function(){
    $post('/Home/Addcomment?articleID=@Model.ID&CommentTxt='+$('.commenttext').val()).always(function()
    {
    $('.comments).Load(/Home/AllComments/articleID=@Model.ID)
    });
    })
    </script>
    
     //*Your article here
    ......
    
     //*Comments section
    <div class="comments">@Html.RenderAction("AllComments", "Home")</div>
    
    <input type="text" class="commenttext" />
    <button class="addComment">Add comment</button>
    

    在您的所有评论视图中

    <ul>
    @foreach(var i in Model)
    <li>
    @i.CommentTxt 
    </li>
    </ul>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 2021-03-29
      相关资源
      最近更新 更多