Я постараюсь сразу перейти к делу. Мне помогли создать ViewModel здесь, на StackOverflow. Он отлично работал в MVC 4, но теперь, когда я конвертирую приложение в MVC 5, он не работает. В коде ничего не изменилось. У меня есть _navigation.cshtml, который является частичным, который отображается в моем Layout.cshtml, и ошибка находится в циклах For в этом частичном. Этот же код отлично работает в MVC 4. Вот код:
Моя ошибка находится на частичной странице во время цикла for, и я получаю сообщение об ошибке Ingredient
в строке:
@foreach (Ingredient ingredient in Model.Ingredients)
и там же на любом другом цикле for там же. Ошибка говорит:
Не удалось найти имя типа или пространства имен «Рецепт» (вам не хватает директивы использования или ссылки на сборку?)
Вот мой код:
Модели/Ingredient.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace XXX.Models
{
public class Ingredient
{
public int IngredientID { get; set; }
public string IngredientNameEs { get; set; }
}
}
Модели/Recipe.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace XXX.Models
{
public class Recipe
{
public int RecipeID { get; set; }
public string RecipeNameEs { get; set; }
}
}
Модели/IdentityModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using Microsoft.AspNet.Identity.EntityFramework;
using XXX.Models;
namespace XXX.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class,
// http://go.microsoft.com/fwlink/?LinkID=317594 for more.
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(): base("DefaultConnection")
{
}
public DbSet<Ingredient> Ingredients { get; set; }
public DbSet<Recipe> Recipes { get; set; }
}
}
ViewModels/NavigationViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XXX.Models;
namespace XXX.ViewModels
{
public class NavigationViewModel
{
public IEnumerable<Ingredient> Ingredients { get; set; }
public IEnumerable<Recipe> Recipes { get; set; }
}
}
Контроллеры/PartialsController.cs
using XXX.Models;
using XXX.ViewModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace XXX.Controllers
{
public class PartialController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Navigation()
{
NavigationViewModel viewModel;
viewModel = new NavigationViewModel();
viewModel.Ingredients = db.Ingredients.Where(i => i.IsProduct != false).ToList();
viewModel.Recipes = db.Recipes.ToList();
return PartialView("_Navigation", viewModel);
}
}
}
Partials/_Navigation.cshtml (звездочки указывают на ошибку рядом с Ingredient и Recipe в цикле For)
@model XXX.ViewModels.NavigationViewModel
@using XXX.Models
//edited for brevity..
<li class="has-dropdown">@Html.ActionLink(XXX.Views.Shared.CultureSwap.Products, "Products", "Ingredient")
<ul class="dropdown">
@*//From NavigationViewModel print out each Product*@
@foreach (*Ingredient* ingredient in Model.Ingredients)
{
<li><a href="/Ingredient/Products/#@ingredient.IngredientNameEs">@ingredient.IngredientNameEs</a></li>
}
</ul>
</li>
<li class="divider"></li>
<li class="has-dropdown">@Html.ActionLink(XXX.Views.Shared.CultureSwap.Recipes, "List", "Recipe")
<ul class="dropdown">
@foreach (*Recipe* recipe in Model.Recipes)
{
<li><a href="/Recipe/List/#@recipe.RecipeNameEs">@recipe.RecipeNameEs</a></li>
}
</ul>
</li>
Опять ошибки гласят:
Не удалось найти имя типа или пространства имен «Ингредиент» (вам не хватает директивы использования или ссылки на сборку?)
Не удалось найти имя типа или пространства имен «Рецепт» (вам не хватает директивы использования или ссылки на сборку?)
Вот скриншот ошибки в Visual Studio Рядом с тем же кодом без ошибок: