c# – MVC 5 ViewModel不像在MVC 4中那样工作
我会尽力直截了当.我在StackOverflow上有一些构建ViewModel的帮助.它在MVC 4中运行良好但现在我将应用程序转换为MVC 5它无法正常工作.代码方式没有任何改变.我有一个_navigation.cshtml,它是在我的Layout.cshtml中呈现的部分,并且错误在该Partial中的For循环内.这个相同的代码在MVC 4中工作正常.这是代码:
我的错误是在for循环期间的部分页面中,我在行中的Ingredient上得到错误: @foreach (Ingredient ingredient in Model.Ingredients) 也可以在同一个地方的任何其他地方循环.错误说:
这是我的代码: 型号/ 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> 错误再读:
以下是Visual Studio中错误的屏幕截图,在相同代码旁边没有错误: 解决方法
您的视图不知道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 ... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 你能动态获取你在oracle apex上的页码吗?
- ruby – 如何在某些类型的参数上使用RSpec正则表达式参数匹
- ruby-on-rails – 启用了rails的Rubocop和Style / Indentat
- ruby-on-rails – Ruby on Rails:使用submit_tag放置类
- Oracle RAC 等待事件
- 在Ruby中排列数组的一部分
- FlashBuilder 中使用Resource Filters 解决多src目录文件重
- struct对象的序列化和反序列化
- ruby-on-rails-3 – 在使用Omniauth(Rails Devise)创建帐户
- Swift - 导航条(UINavigationBar)的使用