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

时间:2015-08-28 05:23:40

标签: c# linq asp.net-mvc-3 asp.net-mvc-4 razor

我正在创建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; }
    }

这是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查询方法

1 个答案:

答案 0 :(得分:1)

这不适用于此:

@foreach (var discussion in Model.DiscussionVM)

因为Model.DiscussionVM不是IEnumerable&lt;&gt;

尝试枚举模型并访问该集合中的覆盖项目的.DiscussionVM

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

<div>@item.DiscussionVM.Discussion_Description</div>
<div>@item.DiscussionVM.Discussion_CreateDate</div>
}