1

I am view specific category of item in a view. I want it to view all products under that category when clicked but I get a null reference exception. I have seperate tables for the category and products.

Here is what I tried. Code in view:

@model IEnumerable<OpabidFarmsLtd.Models.ViewModels.Shop.CategoryVM>

<h3>Categories</h3>

<ul>
<li><a href="#">All</a></li>
@foreach (var item in Model)
{
    <li><a href="/shop/category/@item.Slug.ToLower()">@item.Name</a></li>
}
 </ul>

And my controller (ShopController):

public ActionResult Category(string name)
    {
        // Declare a list of ProductVM
        List<ProductVM> productVMList;


        using (Db db = new Db())
        {
            // Get category id
            CategoryDTO categoryDTO = db.Categories.Where(x => x.Slug == name).FirstOrDefault();
            int catId = categoryDTO.Id;

            // Init the list
            productVMList = db.Procucts.ToArray().Where(x => x.CategoryId == catId).Select(x => new ProductVM(x))
                .ToList();

            // Get category name
            var productCat = db.Procucts.Where(x => x.CategoryId == catId).FirstOrDefault();
            ViewBag.CategoryName = productCat.CategoryName;
        }

        // Return view with list
        return View(productVMList);
    }

Error Message Screenshot

Edit: While looking for what is wrong, I discovered that the method Category never got the parameter at all, I commented all the codes in it and did this:

 public ActionResult Category(string name)
    {
        string cat;
        using (Db db = new Db())
        {
            CategoryDTO dto = db.Categories.Where(x => x.Slug == "test-category").FirstOrDefault();
            cat = dto.Id.ToString();
        }

        return Content(name + cat);
    }

It returned only 8 which was the Id from the database. I know why it was giving me null, it was because it never received any parameter then default to null. What I can't figure out now is why the parameter isn't getting there from my code above. Please help, I am new to this framework.

10
  • Where's the SQL statement that fails? Commented Nov 7, 2019 at 15:41
  • int catId = categoryDTO.Id; Commented Nov 7, 2019 at 15:42
  • 3
    db.Categories.Where(x => x.Slug == name) there mustn't be any CategoryDTO's that satisfy that condition. Commented Nov 7, 2019 at 15:43
  • 2
    You get a Null Reference Exception because the previous line that sets the categoryDTO variable ends with FirstOrDefault. The query didn't find any results, so it assigned the variable the default value for that type (a null). When you try to access a property of a null object you get that exception. Commented Nov 7, 2019 at 15:44
  • 1
    @Davidire and there is a row that has the value referenced by name in the Slug column? Commented Nov 7, 2019 at 16:05

1 Answer 1

2

You try to send "productVMList" to view but you add categoryVM model to html page. I think you should send "categoryVMList" instead of "productVMList" or you should add productVM to your view. Like;

@model IEnumerable<OpabidFarmsLtd.Models.ViewModels.Shop.ProductVM>

Also you should use .Add to add new item to the list. For example,

ProductVMList.Add(object)

Otherwise, put breakpoint to return View(ProductVMList) and check list null or filled.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.