我会尽力直截了当.我在StackOverflow上有一些构建viewmodel的帮助.它在MVC 4中运行良好但现在我将应用程序转换为MVC 5它无法正常工作.代码方式没有任何改变.我有一个_navigation.cshtml,它是在我的Layout.cshtml中呈现的部分,并且错误在该Partial中的For循环内.这个相同的代码在MVC 4中工作正常.这是代码:
我的错误是在for循环期间的部分页面中,我在行中的Ingredient上得到错误:
@foreach (Ingredient ingredient in Model.Ingredients)
也可以在同一个地方的任何其他地方循环.错误说:
The type or namespace name ‘Recipe’ could not be found (are you
missing a using directive or an assembly reference?)
这是我的代码:
型号/ 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(星号表示For Loop中的成分和配方附近的错误)
@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>
错误再读:
The type or namespace name ‘Ingredient’ could not be found (are you
missing a using directive or an assembly reference?)The type or namespace name ‘Recipe’ could not be found (are you
missing a using directive or an assembly reference?)
解决方法
您的视图不知道Ingredient或Recipe的来源,您需要添加对这些类型所在的命名空间的引用,将@using XXX.Models添加到视图的顶部
@model XXX.viewmodels.Navigationviewmodel @using XXX.Models ... @foreach (Ingredient ingredient in Model.Ingredients) { ... }
在旁注中,您似乎有一个半生不熟的视图模型实现.在您的Navigationviewmodel中,您正在引用域模型.通常建议通过视图模型暴露的任何事实上都是视图模型本身.因此,在您的情况下,我将介绍几种新的视图模型来表示成分/配方,即
public class Ingredientviewmodel { ... } public class Recipeviewmodel { ... } public class Navigationviewmodel { public IEnumerable<Ingredientviewmodel> Ingredients { get; set; } public IEnumerable<Recipeviewmodel> Recipes { get; set; } }
这些将在XXX.viewmodels下创建,这意味着您的视图看起来像
@using XXX.viewmodels @model Navigationviewmodel ...