在mvc中将两个数据模型绑定到一个模型

本文关键字:绑定 一个 模型 数据模型 两个 mvc | 更新日期: 2023-09-27 18:10:14

我正在创建MVC 5应用程序,在这个应用程序中,我使用一个var模型连接多个数据表,并将该值绑定到"DiscussionPreview_Model"模型以列出数据

这是控制器类

   public ActionResult Discussion_Preview()
    {
        int Discussion_ID = 1;

        var discussion_preview = (from d in db.AB_Discussion
                                  where d.Discussion_ID == Discussion_ID
                                  join dc in db.AB_DiscussionComments on d.Discussion_ID equals dc.Discussion_ID
                                  join user_discussion in db.AspNetUsers on d.CreatedBy equals user_discussion.Id
                                  join user_comments in db.AspNetUsers on dc.CreatedBy equals user_comments.Id
                                  select new DiscussionPreview_Model
                                  {
                                      Disussion_ID = d.Discussion_ID,
                                      Discussion_CreateDate = d.CreatedDate,
                                      Discussion_CreateBy = user_discussion.UserName,
                                      Discussion_Title = d.Discussion_Name,
                                      Discussion_Description = d.Discription,
                                      Comment_ID = dc.Comment_ID,
                                      Comment_Description = dc.Comment_Discription,
                                      Comment_CreateDate = dc.CreatedDate,
                                      Comment_CreateBy = user_comments.UserName
                                  });

            return View(discussion_preview);
    }

这是我的"DiscussionPreview_Model"模型类

   public class DiscussionPreview_Model
    {
        DiscussionVM model1;
        CommentVM model2;
    }

    public partial class DiscussionVM
    {
        public int Disussion_ID { get; set; }
        public Nullable<System.DateTime> Discussion_CreateDate { get; set; }
        public string Discussion_CreateBy { get; set; }
        public string Discussion_Title { get; set; }
        public string Discussion_Description { get; set; }
        public IEnumerable<CommentVM> Comments { get; set; }
    }
    public partial class CommentVM
    {
        public int Comment_ID { get; set; }
        public Nullable<System.DateTime> Comment_CreateDate { get; set; }
        public string Comment_CreateBy { get; set; }
        public string Comment_Description { get; set; }
    }

This is View Page

 @model IEnumerable<prjkt.Models.DiscussionPreview_Model>
@{
    ViewBag.Title = "Discussion_Preview";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@foreach (var discussion in Model.DiscussionVM)
{
<h3>>@discussion.Discussion_Title</h3>
<div>@discussion.Discussion_Description</div>
<div>@discussion.Discussion_CreateDate</div>
}
@foreach (var item in Model.CommentVM)
{
    <fieldset>
        <legend></legend>

        <h4>Comments</h4>
        <div class="display-field">@item.Comment_Description</div>
        <div class="display-field">@item.Comment_CreateBy : @item.Comment_CreateDate </div>
    </fieldset>
}

但是这个不能正常工作,我尝试了很多方法来克服这个问题

真的很欣赏可以建议一个强大的linq查询方法

在mvc中将两个数据模型绑定到一个模型

这里不行:

@foreach (var discussion in Model.DiscussionVM)

因为模型。讨论虚拟机不是IEnumerable<>

尝试枚举Model并访问集合中到达项的.DiscussionVM

@foreach (var item in Model)
{
<h3>@item.DiscussionVM.Discussion_Title</h3>
<div>@item.DiscussionVM.Discussion_Description</div>
<div>@item.DiscussionVM.Discussion_CreateDate</div>
}