Asp.net mvc 添加注释-一个视图中的两个模型

Asp.net mvc 添加注释-一个视图中的两个模型,asp.net-mvc,Asp.net Mvc,我想在一个视图中添加两个模型:CommentVM和BlogVM。 评论VM-评论, BlogVM-postDetails。 我尝试通过ajax向数据库中添加注释,然后将我的两个模型传递到视图中。但当我试图显示我的页面时,我收到一个错误,即我的对象为空(commentVM) 下面是我的控制器代码 你有什么建议我做错了什么吗? 谢谢你的帮助 // GET: Admin/Blog/kategoria/{name}/post/id [ActionName("post")] public

我想在一个视图中添加两个模型:CommentVM和BlogVM。 评论VM-评论, BlogVM-postDetails。 我尝试通过ajax向数据库中添加注释,然后将我的两个模型传递到视图中。但当我试图显示我的页面时,我收到一个错误,即我的对象为空(commentVM) 下面是我的控制器代码 你有什么建议我做错了什么吗? 谢谢你的帮助

// GET: Admin/Blog/kategoria/{name}/post/id
    [ActionName("post")]
    public ActionResult PostDetails(int id)
    {
        //Declare BlogVM
           BlogVM model;
        CommentVM model2;

        int id2;

             using (Db db = new Db())
             {
                 //Get the page
                 BlogDTO dto = db.Blog.Find(id);


                 //Confirm page exist
                 if (dto == null)
                 {
                     return Content("Taka strona nie istnieje!");
                 }

                 //Init BlogVM
                model = new BlogVM(dto);

            id2 = dto.Id;
           // CommentDTO dto2 = db.Comments.Find(x => x.PostId == id2);

            model2 = new CommentVM();
        }

            var finalItem = new DetailsComment
            {
                Blog = model,
                Comment = model2
            };

        return View("PostDetails", finalItem);

    }
为我的模型编写代码:

public class CommentVM
{
    public CommentVM()
    {

    }

    public CommentVM(CommentDTO row)
    {
        Id = row.Id;
        Name = row.Name;
        Body = row.Body;
        PostId = row.PostId;
        CreatedAt = row.CreatedAt;
    }

    public int Id { get; set; }
    [Required]
    [StringLength(50, MinimumLength = 3)]
    public string Name { get; set; }
    [Required]
    [StringLength(50, MinimumLength = 3)]
    public string Body { get; set; }
    public int PostId { get; set; }
    public DateTime CreatedAt { get; set; }

    //public IEnumerable<CommentVM> CommentDetails { get; set; }
}



public class BlogVM
{
    public BlogVM()
    {

    }

    public BlogVM(BlogDTO row)
    {
        Id = row.Id;
        Title = row.Title;
        Slug = row.Slug;
        Body = row.Body;
        CategoryName = row.CategoryName;
        CategoryId = row.CategoryId;
        CreatedAt = row.CreatedAt;
        Sorting = row.Sorting;
        HasSidebar = row.HasSidebar;
    }

    public int Id { get; set; }
    [Required]
    [StringLength(50, MinimumLength = 3)]
    public string Title { get; set; }
    public string Slug { get; set; }
    [Required]
    [StringLength(int.MaxValue, MinimumLength = 3)]
    [AllowHtml]
    public string Body { get; set; }
    public string CategoryName { get; set; }
    [Required]
    public int CategoryId { get; set; }
    public DateTime CreatedAt { get; set; }
    public int Sorting { get; set; }
    public bool HasSidebar { get; set; }

    public IEnumerable<SelectListItem> Categories { get; set; }
}





public class DetailsComment
{
    public BlogVM Blog { get; set; }
    public CommentVM Comment { get; set; }

    public IEnumerable<CommentVM> CommentDetails { get; set; }
}
公共类CommentVM
{
公共评论VM()
{
}
公共CommentVM(CommentDTO行)
{
Id=row.Id;
Name=row.Name;
Body=row.Body;
PostId=行。PostId;
CreatedAt=row.CreatedAt;
}
公共int Id{get;set;}
[必需]
[StringLength(50,最小长度=3)]
公共字符串名称{get;set;}
[必需]
[StringLength(50,最小长度=3)]
公共字符串体{get;set;}
公共int PostId{get;set;}
public DateTime CreatedAt{get;set;}
//公共IEnumerable CommentDetails{get;set;}
}
公共类BlogVM
{
公共博客虚拟机()
{
}
公共BlogVM(BlogDTO行)
{
Id=row.Id;
标题=行。标题;
Slug=行。Slug;
Body=row.Body;
CategoryName=row.CategoryName;
CategoryId=row.CategoryId;
CreatedAt=row.CreatedAt;
排序=行。排序;
HasSidebar=row.HasSidebar;
}
公共int Id{get;set;}
[必需]
[StringLength(50,最小长度=3)]
公共字符串标题{get;set;}
公共字符串Slug{get;set;}
[必需]
[StringLength(int.MaxValue,MinimumLength=3)]
[allowtml]
公共字符串体{get;set;}
公共字符串CategoryName{get;set;}
[必需]
public int CategoryId{get;set;}
public DateTime CreatedAt{get;set;}
公共整数排序{get;set;}
公共布尔HasSidebar{get;set;}
公共IEnumerable类别{get;set;}
}
公共类详细信息注释
{
公共BlogVM Blog{get;set;}
公共CommentVM注释{get;set;}
公共IEnumerable CommentDetails{get;set;}
}
在我看来

@foreach (var item in Model.CommentDetails)
                {

                    <tr>
                        <td>
                            <div class="ajaxdivtd"></div>
                            @Html.DisplayFor(modelItem => item.Name)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Body)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.CreatedAt)
                        </td>
                    </tr>
                }
@foreach(Model.CommentDetails中的变量项)
{
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.Body)
@DisplayFor(modelItem=>item.CreatedAt)
}

如果根据您的评论,您在每篇文章中都有多条评论,那么您需要做以下几点:

//Declare BlogVM
BlogVM model;
CommentVM model2; //you can probably get rid of this variable since you want multiple comments per post, rather than a single one, but that's up to you
List<CommentVM> commentsModel; //new List object to hold the multiple comments for the post

您的
finalItem
变量只为
Blog
Comment
设置值,而不为
CommentDetails
设置值。为什么在DetailsComment类中有一个CommentVM对象和CommentVM列表?据我所知,对于一篇特定的帖子,你会有很多评论,是吗?是的,我对一篇帖子有很多评论,我试图这么做,但我收到了错误->@foreach(Model.Comment中的var项)->错误:foreach语句无法对CommentVm类型的变量进行操作,因为CommentVm不包含GetEnumerator的公共定义。任何sugestion?var dto2=db.Comments.Find(x=>x.PostId==id2);这一行我得到错误:无法将lambda表达式转换为type object,因为它不是委托类型OK,那么您的
Find
方法可能只允许对象,用于检索多个记录的方法不正确。如果不了解更多有关设置的信息,就很难说出执行此功能的适当方法。在我们的设置中,我们有一个
IQueryable
对象,允许我们使用
.Where
和过滤器,例如
x=>x.PostId==id2
。感谢您的帮助!我把它翻了三倍,但没用。也许是因为我是ASP的新手。在我的应用程序中,我只想在我的视图中传递两个模型:一个带有帖子详细信息(BlogVM有效),另一个带有评论详细信息(CommentVM无效)。如果您有任何建议或示例,请让我知道。您已经通过传递一个包含您的子模型(
Blog
Comment
)的模型(
Comment
/
CommentDetails
)来实现这一点。您的问题不是传递多个模型,而是检索数据以填充
CommentDetails
模型。在我的解决方案中,如果将
CommentDetails=commentsModel
更改为
CommentDetails=new List()
,则应该允许加载视图而不会出错,尽管没有任何实际的注释进行迭代和显示。
id2 = dto.Id;
var dto2 = db.Comments.Find(x => x.PostId == id2);
//Convert dto2 from an list/enumerable of CommentDTO to a List<CommentVM>, perhaps via AutoMapper (or other such utility), or even manually via a foreach
commentsModel = YourConversionMethod(dto2);
var finalItem = new DetailsComment
{
    Blog = model,
    Comment = model2,
    CommentDetails = commentsModel
};