I am developing e commerce project with asp.net core 6 mvc. My controller;
[HttpGet]
public async Task<IActionResult> ProductListing(int categoryId)
{
var products = await _productService.TGetByCategoryId(categoryId);
List<ProductViewModel> productList = new List<ProductViewModel>();
foreach (var item in products)
{
ProductViewModel model = new ProductViewModel();
model.ProductId = item.Id;
model.Name = item.Name;
model.Price = item.Price;
model.ImageUrl = item.ImageUrl;
productList.Add(model);
}
var category = await _categoryService.TGetById(categoryId); //in this line I had System.NullReferenceException: 'Object reference not set to an instance of an object.' exception.
//ViewBag.CategoryName = category.CategoryName;
return View(productList);
}
How can I fix it ?
I am expecting there is no exception, I couldnt get how this include the exception.