Dylan K
Dylan K

Reputation: 13

ASP.NET MVC View not receiving list passed into it

I am working on a relatively simple ASP.NET MVC application in C# in Visual Studio. In this situation I am trying to pass a list of objects into my view and then display their names, but I keep getting an error

Categories does not exist in the current context

Here is the controller:

public IActionResult Index()
{
    List <CheeseCategory> categories = context.Categories.ToList();
    return View(categories);
}

And here is the View:

@model CheeseMVC.Models.CheeseCategory

@if (categories != null)
{
    foreach(var category in categories)
    {
        <ul>@category</ul>
    }
}

I have done this before, and I can't figure out what is going wrong here, does anyone have any ideas?

Upvotes: 1

Views: 61

Answers (1)

Tim
Tim

Reputation: 2098

You passed in a List<CheeseCategory>, but in your Razor file, you said model would be just a CheeseCategory. The @model declaration needs to match what you're passing in.

Also, replace categories with Model in the if statement in the Razor file.

Upvotes: 3

Related Questions