MVC 5 ViewModel not working as it was in MVC 4
There is a web.config
file located in the Views
directory. In it the namespaces that should be available for the views are listed. Did you add a namespace to the views web.config
in your mvc4 proj that you are now missing in the mvc5 proj?
The listing in the views web.config
is a kind of global using
statements that applies to all views.
Your view doesn't know where Ingredient
or Recipe
come from, you need to add a reference to the namespace which those types are under, add @using XXX.Models
to the top of your view
@model XXX.ViewModels.NavigationViewModel
@using XXX.Models
...
@foreach (Ingredient ingredient in Model.Ingredients)
{
...
}
On a side-note you appear to have a half-baked view model implementation. In your NavigationViewModel
you are referencing, which appear to be, domain models. It's generally recommended that anything exposed via a view model, is in actual fact, a view model itself. So in your case, I would introduce a couple of new view models to represent an Ingredient
/ Recipe
i.e.
public class IngredientViewModel
{
...
}
public class RecipeViewModel
{
...
}
public class NavigationViewModel
{
public IEnumerable<IngredientViewModel> Ingredients { get; set; }
public IEnumerable<RecipeViewModel> Recipes { get; set; }
}
These would be be created under the XXX.ViewModels
which would mean your view would look like
@using XXX.ViewModels
@model NavigationViewModel
...