4

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?)

Chathz
  • 723
  • 4
  • 16
  • 41
  • 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? –  Aug 27 '15 at 05:29
  • but only I have one discussion title and discussion description – Chathz Aug 27 '15 at 05:30
  • 1
    No you don't, you have a collection (that's what `IEnumerable` is) –  Aug 27 '15 at 05:31
  • but I want to display one discussion title and discussion description and then loop the comments , how can I do that ? – Chathz Aug 27 '15 at 05:32
  • 2
    Then you need to redesign your model. You want properties such as `Title`, `Description` etc, and `IEnumerable Comments` where `Comment` contains properties such as `CreateBy` and `Description` –  Aug 27 '15 at 05:34
  • Thanks I did you said this is code snippet https://dotnetfiddle.net/XOkleA But Then Im having problem in Contoller class how can I void it – Chathz Aug 27 '15 at 06:12
  • You cant use a DotNetFiddle to refer to your database context :) You just need to modify your query so that you get the first `AB_Discussion` that matches `Discussion_ID` and populate the main properties of the model, and then get all associated comments to populate the `Comments` collection. And stop that awful naming - just `ID` not `Disussion_ID` :) –  Aug 27 '15 at 06:19
  • bit of lack of knowledge to modify my controller class linq query can you help me with that – Chathz Aug 27 '15 at 06:52
  • If you modify you fiddle to add the models associated with `AB_Discussion` and `AB_DiscussionComments` I'll have a look for you a bet later –  Aug 27 '15 at 07:01
  • okay , thanks , As I feel since comments properties out of the DiscussionPreview_Model Im getting error in current Linq query as `'DiscussionPreview_Model' does not contain a definition for 'Comment_ID,...'` – Chathz Aug 27 '15 at 08:09
  • I tried to do this in this way just look at the code snippet https://dotnetfiddle.net/xroRV5 but then its not show the Discussion_Description or Discussion_CreateBy only it shows Discussion_Title – Chathz Aug 27 '15 at 11:39
  • I can't help much because you have not posted the data models, but start by breaking this down into 2 parts. Get the `discussion` object from `AB_Discussion` based on the `Discussion_ID` value and populate `Title`, `Description` etc. The query the `AB_DiscussionComments` tables to get the comments associated with `Discussion_ID` value and populate the `Comments` property with the results. –  Aug 27 '15 at 11:55

2 Answers2

4

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.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
1

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.

Community
  • 1
  • 1
Ravi
  • 475
  • 4
  • 12
  • Thnks.I tried both your options seems like not working – Chathz Aug 27 '15 at 06:46
  • `'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?)` – Chathz Aug 27 '15 at 07:05
  • Please put class with full path (IEnumerable)--> IEnumerable<.DiscussionPreview_Model> Please let me know if still you are facing any problem – Ravi Aug 27 '15 at 07:12
  • That how its existing, with full path :(( – Chathz Aug 27 '15 at 08:11
  • for me @Html.DisplayFor(m=>m.ElementAt(0).Discussion_Title) is working – Ravi Aug 27 '15 at 09:24
  • @Ravi I have same error please help me to solve same error I need to show as master header of the page and details down this is my question link https://stackoverflow.com/questions/62012068/cs1061-ienumerableorders-tables-does-not-contain-a-definition-for-labviewr , but i dont need to loop it and i need to get one value only – Abdullah May 25 '20 at 23:31