4
votes

I want to create online forum like below image


enter image description here


this is my Controller method

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

Model Class

public class DiscussionPreview_Model
{
    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 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<prjct.Models.DiscussionPreview_Model>

@{
    ViewBag.Title = "Discussion_Preview";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

 <h3>@Html.DisplayFor(model => model.Discussion_Title)</h3>

<div>@Html.DisplayFor(model => model.Discussion_Description)</div>
<div>@Html.DisplayFor(model => model.Discussion_CreateDate)  @Html.DisplayFor(model => model.Discussion_CreateDate)</div>

@foreach (var item in Model)
{

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

When debug above application I'm getting below error message

'IEnumerable' does not contain a definition for 'Discussion_Title' and no extension method 'Discussion_Title' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)

2
Your DisplayFor() helpers need to be inside the foreach loop. But my best guess is what you really want to do is display a DiscussionPreview_Model and a collection of all comments associated with it?user3559349
but only I have one discussion title and discussion descriptionChathz
No you don't, you have a collection (that's what IEnumerable is)user3559349
but I want to display one discussion title and discussion description and then loop the comments , how can I do that ?Chathz
Then you need to redesign your model. You want properties such as Title, Description etc, and IEnumerable<Comment> Comments where Comment contains properties such as CreateBy and Descriptionuser3559349

2 Answers

4
votes

Your model is of type IEnumerable<prjct.Models.DiscussionPreview_Model> and you're trying to use it like prjct.Models.DiscussionPreview_Model, which doesn't work. You will have to loop the IEnumerable and then use the Html.DisplayFor for each of the elements inside the IEnumerable, like you're doing in the comments part.

1
votes

You are trying to use as

<h3>@Html.DisplayFor(model => model.Discussion_Title)</h3>

but here your model is IEnumerable, so you are having two options.

Option 1

If you are sure that Discussion_Title, Discussion_Description, and Discussion_CreateDate is same for all record then you have to use as

<h3>@Html.DisplayFor(model => model.ElementAt<DiscussionPreview_Model>(0).Discussion_Title)</h3>

and same for other 2 column, but in this case at least one record must be there otherwise you have to check for count of record before using it.

Option 2

Try to create the model in such a way that like

public class Discssion
{
    public string Discussion_Title { get; set; }
    public string Discussion_Description { get; set; }
    public string Comment_CreateBy { get; set; }
    public List<DiscussionPreview> DiscussionPreviews{ get;set;}
}

public class DiscussionPreview
{
    public class Comment_Description{ get; set; }
    .....
}

and use it in same way in your view.

Let me know you need more details.