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

时间:2017-04-05 14:24:56

标签: asp.net-mvc viewmodel

我有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 View

上使用它们
@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>
}

但是当我跑的时候有一个错误:

  

传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [ShareImage.Models.PostViewModel]',但此字典需要类型为'ShareImage.Models.BigViewModel'的模型项。 / p>

这是我的控制者:

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的代码。但我认为这并不重要,因为我稍后会完成它。

我已经尝试了很多方法来解决它,但它仍然无法工作。 请帮助我,谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个。这可能对你有帮助。

控制器更改: -

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

答案 1 :(得分:-1)

部分视图。

部分视图@Html.Partial是剃刀语法的定义方法,用于呈现或合并您想要的视图。在你的情况下,这是一个简单的方法。

@Html.Partial("OtherViewName") // add this where you have to render view.