ASP MVC中的多形式多模型

时间:2016-06-25 11:33:09

标签: c# asp.net-mvc

各位大家好,

我正在尝试学习C#ASP MVC,我有一点问题。

我想发布一个简单的博客。

所以我想在同一页面上显示一篇文章和一个表单来评论帖子。

我有一个帖子模型和另一个评论。

这是我的模特:

comment.cs

public class Comment
        {
            public Guid id { get; set; }
            public string content { get; set; }
            public int numComment { get; set; }
            public virtual Post postId { get; set; }
            public virtual ApplicationUser author { get; set; }
        }

post.cs

public class Post
    {
        public Guid id { get; set; }
        public string title { get; set; }
        public string content { get; set; }

        public virtual ICollection<ApplicationUser> author { get; set; }
        public virtual ICollection<Comment> comments { get; set; }
    }

我使用帖子模型来显示文章的详细信息,因此我无法使用评论模型来注册评论。

最佳做法是什么?我已经在同一个类中看到了@ html.action和部分视图或复杂的viewModel以及post和comment模型。

非常感谢你给我的所有线索。

*对不起英文错误,我是法国人。

2 个答案:

答案 0 :(得分:0)

public class Comment
{
     public Guid id { get; set; }
     public string content { get; set; }
     public int numComment { get; set; }
     public virtual Post postId { get; set; }
     public virtual ApplicationUser author { get; set; }
 }

public class Post
{
     public Guid id { get; set; }
     public string title { get; set; }
     public string content { get; set; }
     public virtual ICollection<ApplicationUser> author { get; set; }
     public virtual ICollection<Comment> comments { get; set; }
     public Comment Commentobject {get;set;}
 }

并使用视图模型concept.Declare使用@model关键字在视图中发布模型。获取控制器中表单帖子上的值

答案 1 :(得分:0)

我创建了一个非常基本的示例,其中包含显示文章的页面。

  1. 该页面使用部分视图来检索评论列表
  2. 该页面使用AJAX插入新评论
  3. 如果评论已成功插入,我们会在步骤1中调用部分视图来刷新评论列表。
  4. 由于jQuery和AJAX,所有事情都发生在一个页面上:

    代码第一个数据对象:

    public class Comment
    {
        public Guid id { get; set; }
        public string content { get; set; }
        [System.ComponentModel.DataAnnotations.Schema.ForeignKey("post")]
        public Guid? PostId { get; set; }
        public int numComment { get; set; }
        public virtual Post post { get; set; }
        public virtual ApplicationUser author { get; set; }
    }
    
    public class Post
    {
        public Guid id { get; set; }
        public string title { get; set; }
        public string content { get; set; }
        public virtual ICollection<ApplicationUser> author { get; set; }
        public virtual ICollection<Comment> comments { get; set; }
    }
    
    public class DatabaseContext : DbContext
    {
        public DatabaseContext() : base("name=Connection")
        {
        }
        public DbSet<Comment> Comments { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<ApplicationUser> ApplicationUsers { get; set; }
    }
    

    MVC控制器:

    public class NewsController : Controller
    {
        private DatabaseContext db = new DatabaseContext();
    
        public ActionResult Index()
        {
            Post firstPost = db.Posts.FirstOrDefault();
            return View(firstPost);
        }
    
        public PartialViewResult GetComments(string postId)
        {
            Guid post = Guid.Parse(postId);
            List<Comment> comments = db.Comments.Where(c => c.PostId == post).ToList();
            return PartialView("~/Views/News/_Comments.cshtml", comments);
        }
    
        [HttpPost]
        public JsonResult AddComment(string postId,string content)
        {
            Guid post = Guid.Parse(postId);
    
            var comment = new Comment
            {
                id = Guid.NewGuid(),
                PostId = post,
                content = content
            };
    
            db.Comments.Add(comment);
            db.SaveChanges();
    
            return Json(new {Created = true }, JsonRequestBehavior.AllowGet);
        }
    }
    

    评论的部分视图(称为_Comments.cshtml):

    @model List<Newspaper.Models.Comment>
    
    @foreach(var comment in Model)
    {
        <div class="row">
            <div class="col-md-12 well">@comment.content</div>
        </div>
    }
    

    主要观点:

    @model Newspaper.Models.Post
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    
    <script type="text/javascript">
        $(function () {
    
            GetCommentsForArticle();
    
            function GetCommentsForArticle() {
                debugger;
                var id = '@Model.id';
                $.get("/News/GetComments?postId=" + id, function (data) {
                    $("#comments").empty();
                    $("#comments").append(data);
                });
            }
    
            $("#btnPostComment").click(function () {
                var id = '@Model.id';
                var comment = $("#comment").val();
    
                $.ajax({
                    url: "/News/AddComment",
                    type: "Post",
                    data: { 'postId': id , 'content':comment},
                    success: function (data) {
                        debugger;
                        GetCommentsForArticle();
                    },
                    error: function (e) {
                        alert('An error occured while posting a comment');
                    }
                });
            });
        });
    </script>
    
    <div class="container">
        <div class="row">
            <div class="col-md-12"><h1>Article - @Html.DisplayFor(model => model.title) </h1></div>
        </div>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-md-12">@Html.DisplayFor(model => model.content)</div>
        </div>
    </div>
    <div id="comments" class="container">
    </div>
    <div class="container">
        <h3>Add another comment</h3>
        Comment<input type="text" id="comment" />
        <input type="button" id="btnPostComment" value="Post Comment" />
    </div>