如何在 1 个视图中使用多个模型

How to use mutiple Model in 1 View

我有 2 个 ViewModel

 public class PostViewModel
{
    [Key]
    public int PostID { get; set; }
    public int UserID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Picture { get; set; }
    public DateTime CreateDate { get; set; }
    public string Username { get; set; }
    public string Fullname { get; set; }
    public string CategoryID { get; set; }
    public int LikeNumber { get; set; }
}
public class CommentViewModel
{
    [Key]
    public int CommentID { get; set; }
    public int UserID { get; set; }
    public string Username { get; set; }
    public int PostID { get; set; }
    public string Content { get; set; }
}

然后我创建了另一个 ViewModel

public class BigViewModel
{
    public List<CommentViewModel> CommentViewModels { get; set; }
    public List<PostViewModel> PostViewModels { get; set; }
}

我想在 1 个视图中使用它们

@model ShareImage.Models.BigViewModel
@foreach (var item in Model.PostViewModels.OrderByDescending(x=>x.CreateDate))
{


    <div class="row">

        <div class="col-lg-12">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h3><b>TÀI KHOẢN:</b> @item.Username</h3>
                    <h3><b>NGÀY ĐĂNG:</b>  @item.CreateDate</h3>
                </div>
                <div class="panel-body">
                    <div class="text-center">
                        <img src="@Url.Content(item.Picture)" class="rounded mx-auto d-block" alt="@item.Description" style="width:900px;height:600px" />
                    </div>
                    <hr />
                    <div class="panel-footer">
                        <h2>TIÊU ĐỀ:  @item.Title</h2>
                        <h2>Mô tả: @item.Description</h2>
                    </div>
                </div>
                @Html.TextBox("Bình luận...") <button class="btn btn-success">Comment</button>

            </div>
            @foreach (var item1 in Model.CommentViewModels)
            {
                <div>@item1.Username</div>
                <div>@item1.Content</div>
            }
        </div>
    </div>
}

但是当我运行时出现错误:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[ShareImage.Models.PostViewModel]', but this dictionary requires a model item of type 'ShareImage.Models.BigViewModel'.

这是我的控制器:

public ActionResult Index()
    {
        var db = new ShareImageDbContext();
        List<PostViewModel> model =new List<PostViewModel>();            
        var q = (from a in db.Posts
                 join b in db.Users on a.UserID equals b.UserID 
                 select new
                 {
                     postID=a.PostID,
                     title=a.Title,
                     description=a.Description,
                     createDate=a.CreateDate,
                     picture=a.Picture,
                     username=b.Username,
                 }).ToList();
        foreach (var item in q)
        {
            model.Add(new PostViewModel()
            {
                PostID = item.postID,
                Title=item.title,
                Description=item.description,
                Picture=item.picture,
                CreateDate=item.createDate,
                Username=item.username,
            });
        }
        return View(model);
     }

我还没有完成 Controller 中 CommentViewModel 的代码。不过我觉得不重要,因为我会在后面补全

我试了很多方法都解决不了,还是不行。 请帮助我,谢谢!

试试这个 one.This 可能对你有帮助。

控制器变更:-

 List<PostViewModel> model =new List<PostViewModel>(); 

  BigViewModel bigModel=new BigViewModel();
  bigModel.PostViewModels = model;
  return View(bigModel);

查看更改:-

@foreach (var item in Model.BigViewModel.PostViewModels.OrderByDescending(x=>x.CreateDate))
{
}