首页 > 解决方案 > 如何在html表asp.net mvc中显示组数据

问题描述

嗨,我有这张表,我想在其中显示来自控制器的组数据,它返回按会议 ID 分组的对象

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

看法

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

但是当我运行应用程序时,它会抛出一个错误说

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]'.

这是我的模态

{
    [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; }

我以前没有使用 SQL groupdata 的经验,我尝试在线寻找解决方案,但到目前为止没有任何效果,我该如何解决这个问题?谢谢

标签: c#sqlasp.net-mvclinq

解决方案


你的模型应该是@model IEnumerable<IGrouping<int, MinuteOfMeeting>>

  • var res = db.MinuteofMeet.GroupBy(x => x.MeetID) 而不是 ToList()

推荐阅读