samedi 30 octobre 2021

How to show my entities depending on multiple "CheckBox Inputs "in ASP.NET Core?

I have two entities named Course.cs and CourseGroup.cs and each Course.cs has one CourseGroup.cs inside itself. I've fetched all my CourseGroups inside the ViewPage using a foreach loop and placed them with their values inside input tags as following:

@foreach (var item in courseGroups.Where(c => c.ParentId == null))
{
        <li>
        <input value="@item.CourseGroupId" type="checkbox" name="groupIds" id="cat-1">
        <label for="cat-1"> @item.CourseTitle </label>

        @if (courseGroups.Any(c => c.ParentId == item.CourseGroupId))
        {
                <ul>
                @foreach (var sub in courseGroups.Where(c => c.ParentId == 
                        item.CourseGroupId))
                        {
                                 <li>
                                         <input value="@sub.CourseGroupId" type="checkbox" name="groupIds" id="cat-1">
                                 <label for="cat-1"> @sub.CourseTitle </label>
                                 </li>
                        }
                </ul>
                        }

         </li>

         }

Now, I want to show my Course.css depending on the checked inputs, for example: assume that I have one Course in my DataBase with "Learning Advanced React Native" title, and it has relarions with CourseGroup JavaScript and SubCourseGroup React. So when my application is running, I click on the CheckBox inputs JavaScript and 'React', but it shows me nothing while when I just click on one of them, it will show me the courses!

Bellow is my method function and how to fetch my Courses.cs:


public List<ShowListCoursesViewModel> GetListCoursesByFilters(int pageId = 1, string? filter = null, string buyType = "all",
            string sortType = "NewestDate", decimal startPrice = 0,
            decimal endPrice = 0, List<int>? groupIds = null, int take = 0, bool IsMainPage = true)
{
IQueryable<Course> result = _context.Courses;
.
.
.
if (groupIds != null && groupIds.Any())
{
        foreach (var item in groupIds)
        {
                result = result.Where(c => c.CourseGroupId == item || c.CourseSubGroupId == item);
        }
}
.
.
.
}

and my ActionMethod is as following:

#nullable enable
        public IActionResult Index(int pageId = 1, string? filter = null, string buyType = "all",
            string sortType = "newestDate", decimal startPrice = 0,
            decimal endPrice = 0, List<int>? groupIds = null, int take = 0, bool IsMainPage = true)
        {
            ViewData["byType"] = buyType;
            ViewData["CourseGroups"]=_courseServices.GetGroupsAndSubGroups();
            return View(_courseServices.GetListCoursesByFilters(pageId,filter,buyType,
                sortType,startPrice,endPrice,groupIds,take=9,IsMainPage=false));
        }
    }

Would anybody help?




Aucun commentaire:

Enregistrer un commentaire