0
votes

I'm try to show single group by id, sub groups that have that group id as parent id and product that have group id or parent id.

code of service:

    public List<CompanyProductGroup> GetPorductGroupById(int id)
    {

        return _context.CompanyProductGroups
             .Where(c => c.CompanyProductGroupId == id && c.IsDelete != true || c.ParentId == id).ToList();
    }

controller:

    [Route("group/{title}/{id}")]
    public IActionResult Group(string title, int id)
    {
        ViewBag.PageId = id;
        return View(_companyService.GetPorductGroupById(id));
    }

view:

@foreach (var prod in Model.Where(p => p.ParentId == ViewBag.PageId)){
    <section style="height:150vh" class="has-shadow section2">
        <div class="white"></div>
        <div class="pgHeader">
            <div class="headwrap">
                <div class="right-side">
                    <p>
                        @prod.CompanyProductGroupDes
                    </p>
                </div>
                <div class="center-side">
                    <div class="center-side-inner"></div>
                </div>
                <div class="left-side">
                    <img src="~/CompanyProductGroup/image/@prod.CompanyProductGroupPic" alt="@prod.CompanyProductGroupTitle" />

                </div>

            </div>

        </div>
        <div class="clearfix"></div>

        <div class="wid-box">


            @if (prod.CompanyProduct != null)
            {
                @foreach (var gr in prod.CompanyProduct.Where(c => c.SubGroupId == prod.CompanyProductGroupId))
                {
                    <div class="wid-50">
                        <img src="~/Content/img/tires.png" />
                        <div class="wid-50-content">
                            <h2>Prod Title</h2>
                            <p>
                                لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ و با استفاده از طراحان گرافرا برای طراحان رایانهای .
                            </p>
                            <a href="#">بیشتر</a>
                        </div>
                    </div>
                }
            }
        </div>



    </section>

}

I have error on inner foreach, that show me : ArgumentNullException: Value cannot be null. Parameter name: source System.Linq.Enumerable.Where(IEnumerable source, Func predicate)

But I have data in my table. please help me to solve this problem. Thanks in advance

1

1 Answers

0
votes

Not clear about the relationships between your models , but if you want to load related Data , you could use the Include method to specify related data to be included in query results. Try the modification like below :

public List<CompanyProductGroup> GetPorductGroupById(int id)
{

    return _context.CompanyProductGroups.Include(c=>c.CompanyProduct)
         .Where(c => c.CompanyProductGroupId == id && c.IsDelete != true || c.ParentId == id).ToList();
}

Please refer to the official documentation Loading Related Data.