简体   繁体   中英

How to display group data in html table asp.net mvc

Hi i have this table where i want to show group data from controller, it returns objects grouped by meeting id

var res = db.MinuteofMeet.GroupBy(x => x.MeetID).ToList();

view

                    {
                        int i = 1;
                        foreach (var item in Model)
                        {
                            DatContext dbs = new DatContext();
                    <tr>
                        <td hidden>
                            @Html.DisplayFor(modelItem => item.MeetID)
                        </td>
                         <td>
                            @Convert.ToString(string.Format("{0:dd-MM-yyyy}", item.CDate))
                        </td>
                        <td>
                      
                            @Html.DisplayFor(modelItem => item.SubjectDetail)
                        </td>

                        <td>
                            @Convert.ToString(string.Format("{0:dd-MM-yyyy}", item.Meetdate))
                        </td>

                        <td>
                            @*@Html.DisplayFor(modelItem => item.ReviewedBy)*@
                            Demo

                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.responsible)
                        </td>
                        <td hidden>  @Html.DisplayFor(modelItem => item.action_type)</td>

                        <td>
                            <img data-id="@item.Comment" data-toggle="modal" data-target="#commentsmodal" class="datimage" src="~/MatAssets/images/plus.png">
                        </td>

but when I run the application its throwing an error saying

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.Linq.IGrouping`2[System.Int32,SmartBookingPro.Models.MinuteOfMeeting]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[SmartBookingPro.Models.MinuteOfMeeting]'.

and this is my modal

{
    [Table("tbl_minofmeet")]
    public class MinuteOfMeeting
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        public int MeetID { get; set; }
        public string RoomName { get; set; }
        //public string Client { get; set; }
        //public string Subject { get; set; }
        public string Minutes { get; set; }
        public string Comment { get; set; }
                   
        public string ReviewedBy { get; set; }
        public DateTime? CDate { get; set; }
        public string UserID { get; set; }
        public string Minstatus { get; set; }
        public string Task_status { get; set; }
        public DateTime? Meetdate { get; set; }
       
       

        public string responsible { get; set; }
        public string action_type { get; set; }
      
        public string SubjectDetail { get; set; }

I have no prior experience with SQL groupdata and I tried looking for solutions online but nothing is working so far, how can I fix this? thanks

Your model should be @model IEnumerable<IGrouping<int, MinuteOfMeeting>>

  • var res = db.MinuteofMeet.GroupBy(x => x.MeetID) instead of ToList()

For your var res: If you remove the part of .GroupBy(x => x.MeetID) It will be a List of MinutesOfMeeting

Now your var res is most likely a List<IGrouping<int, MinutesOfMeeting>>

After a GroupBy, you could / should do a Select like: .Select(mom => new MintuesOfMeetingModel { MeetId = mom.FirstOrDefault() }).ToList()

You might need to make a new model to implement this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM