jeudi 27 août 2015

bind two data models to one model in mvc

I'm creating MVC 5 application, in this application I'm joining multiple data table using one var model and bind that values to "DiscussionPreview_Model" model to list down data

this is controller class

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

this is my "DiscussionPreview_Model" model class

   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<albaraka.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>
}

but this is not working properly, I tried to overcome this problem in many ways

Really appreciate can suggest a strong linq query approach

Aucun commentaire:

Enregistrer un commentaire