نمایش اطلاعات گروه بندی شده بر اساس نام گروه

8/1/2020 MVC
1761

اطلاعات به صورت والد فرزند در جدول بانک اطلاعاتی ذخیره شده است، میخواستم در یک صفحه نمایش داده شود به نحوی که نام دسته در بالا و اجزاء آن در زیر هر گروه نمایش داده شود.

در این پروژه اطلاعات پایه در project.Bussines , و اطلاعات Repository در پروژه  project.Repository اجرا میشود

در Model هم داشتم 

 public class UsefullLink
    {
        public int Id { get; set; }
        [Display(Name="متن")]
        public string Name { get; set; }
        [Display(Name = "تصویر")]
        public string Image { get; set; }
        [Display(Name = "ادرس لینک")]
        public string LinkUrl { get; set; }
         [Display(Name = "توضیحات")]
        public string Description { get; set; }
        [Display(Name = "مربوط به دسته")]
        public int UseFullLinkTypeId { get; set; }
        [Display(Name = "مربوط به دسته")]
        public UseFullLinkType UseFullLinkType { get; set; }
    }

public class UseFullLinkType
    {
        public int Id { get; set; }
        [Display(Name = "نام")]
        [Required, DataType(DataType.Text)]
        public string Name { get; set; }
        [Display(Name = "توضیحات")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }
        public ICollection<UsefullLink> UsefullLinks { get; set; }
    }

در model.bussines این کلاس رو اضافه کردم 

 public class Group<k, T>
    {
       public k Key;
       public IEnumerable<T> Values;
    }

در repository 

   public IList<Group<String, UsefullLink>> GetAllGroupActive()
        {
            return (from b in _db.UsefullLinks
                                              group b by b.UseFullLinkType.Name into g
                                              select new Univer.Business.Model.Group<string, UsefullLink> { Key = g.Key, Values = g }).ToList();
          
        }

البته اینترفیس IusefullLinkTypeRepository  رو هم داریم که متد بالا را در خود دارد.

 

در view  این 

@using Univer.Business.Model
@model List<Group<string, Univer.Model.UsefullLink>>
@{
    ViewData["Title"] = "ServiceTebbedTable";
}
@section Styles{
    <style>
        #cut-diamond {
            border-style: solid;
            border-color: transparent transparent #10385a transparent;
            border-width: 0 25px 25px 25px;
            height: 0;
            width: 50px;
            box-sizing: content-box;
            position: relative;
            margin: 20px 0 50px 0;
            color: white;
        }

            #cut-diamond:after {
                content: "";
                position: absolute;
                top: 25px;
                left: -25px;
                width: 0;
                height: 0;
                border-style: solid;
                border-color: #10385a transparent transparent transparent;
                border-width: 70px 50px 0 50px;
            }
    </style>
 }
<div class="container">
    <div class="row UsefullLink ">
        <div class="card ">
            <div class="card-header"> <a asp-action="ServiseTable" asp-controller="Home" asp-area="">میز</a></div>
            <ul class="row" style="margin-left:25px ;margin-right:25px;">

                @foreach (var group in Model)
                {
                    <li class="row col-md-12" id="cut-diamond"> @group.Key</li>
                    <li class="m-1"></li>
                    foreach (var link in group.Values)
                    {
                    <li class="col-md-4 mt-2  justify-content-center">
                         <a href="@link.LinkUrl" class="justify-content-center" target="_blank"> @link.Name</a>

                        </li>

                    }
                }
            </ul>
        </div>
    </div>
</div>

و در Controller 

 

  public IActionResult ServiceTebbedTable()
        {
            var item =  _usefullLinkRepository.GetAllGroupActive();
            return View(item);
        }