In my .net core 8 mvc c# project, when I click on the category, the data comes as null listing the products in the relevant category.
when clicking on the category of the products listed on the homepage, I cannot list the products in that category, the code I wrote is below
My Extension
public static string CategorySeoUrl(this Category entity)
{
if (entity == null)
{
return "";
}
if (entity.ParentCategory == null)
{
var title = FriendlyURLTitle(entity.Name).ToLower().Replace("--", "-");
return $"/cat/{title}-C{entity.Id}";
}
else
{
var title = FriendlyURLTitle(entity.Name).ToLower().Replace("--", "-");
var Cattitle = FriendlyURLTitle(entity.ParentCategory.Name).ToLower().Replace("--", "-");
return $"/cat/{Cattitle}-{title}-C{entity.Id}";
}
}
Controller
[Route("cat/{title}-C{id}")]
public async Task<IActionResult> Category(long ID)
{
var catBlog = await _db.blogPosts.Where(x => x.Id == ID)
.Include(x => x.Category)
.Include(x => x.Author)
.Include(x => x.PostImages)
.FirstOrDefaultAsync(x => x.Id == ID);
return View(catBlog);
}
`Url
@Common.Extensions.BlogSeoUrl(Model)
View
@model CategoryViewModel
@using Common
@using X.PagedList.Mvc.Core;
@using X.PagedList;
@using X.PagedList.Mvc.Core.Fluent
@using X.PagedList.Web.Common
@{
ViewData["Title"] = "Haber Kategori";
var pageUrl = Common.Extensions.CategorySeoUrl(Model.Category);
var urlOrd = Context.Request.Query["order"];
var qstring = Context.Request.QueryString.DoString();
var BrandReage = qstring.Contains("categoryID") || qstring == "" ? "?" : "&";
var pageCategory = qstring.Contains("categoryID") ? $"&categoryID={Context.Request.Query["categoryID"]}" : "";
var attrs = ViewBag.Attrs as List<Database.BlogAttribute>;
}
@if (@Model.BlogPost.TotalItemCount > 0)
{
<div class="shop-catalogue grid-view">
<div class="row row-10 items-grid">
@{
if (Model.BlogPost.Any())
{
foreach (var item in Model.BlogPost)
{
await Html.RenderPartialAsync("/Views/Partial/_BlogNewsPartial.cshtml", item);
}
}
}
</div>
<!-- end row -->
</div>
<!-- end grid mode -->
}
else
{
<div id="divUrunYok" style="width: 100%;">
<img src="/image/tr.png" style="display: block; margin: 0px auto;" alt="BLOG BULUNMAMAKTADIR">
</div>
}
<div class="clear"></div>
<div class="row">
<div class="col-12">
@Html.PagedListPager(Model.BlogPost, _page => $"{pageUrl}?page={_page}{pageCategory}", new PagedListRenderOptions
{
LiElementClasses = new string[] { "page-item" },
PageClasses = new string[] { "page-link" },
ContainerDivClasses = new string[] { "pagination mt-3 justify-content-center pagination_style1" },
Display = PagedListDisplayMode.IfNeeded,
//DisplayItemSliceAndTotal = true
})
</div>
</div>