MVC 5 ViewModel不像在MVC 4中那样工作

Eri*_*ard 7 c# runtime-error asp.net-mvc-4 asp.net-mvc-viewmodel asp.net-mvc-5

我会尽力直截了当.我在StackOverflow上有一些构建ViewModel的帮助.它在MVC 4中运行良好但现在我将应用程序转换为MVC 5它无法正常工作.代码方式没有任何改变.我有一个_navigation.cshtml,它是在我的Layout.cshtml中呈现的部分,并且错误在该Partial中的For循环内.这个代码在MVC 4中工作正常.这是代码:

我的错误是在for循环期间的部分页面中,我Ingredient在行中得到错误:

@foreach (Ingredient ingredient in Model.Ingredients)
Run Code Online (Sandbox Code Playgroud)

也可以在同一个地方的任何其他地方循环.错误说:

找不到类型或命名空间名称'Recipe'(您是否缺少using指令或程序集引用?)

这是我的代码:

型号/ 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; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

型号/ 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; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

型号/ 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; }

        }
    }
Run Code Online (Sandbox Code Playgroud)

的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; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

控制器/ 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);
             }
         }
    }
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

错误再读一遍:

找不到类型或命名空间名称'Ingredient'(您是否缺少using指令或程序集引用?)

找不到类型或命名空间名称'Recipe'(您是否缺少using指令或程序集引用?)

以下是Visual Studio中错误的屏幕截图,在相同代码旁边没有错误:

在此输入图像描述

And*_*bel 6

目录中有一个web.config文件Views.其中列出了应该可用于视图的名称空间.您是否web.config在mvc4 proj中的视图中添加了一个名称空间,您现在在mvc5项目中缺少该名称空间?

视图中的列表web.config是一种using适用于所有视图的全局语句.


Jam*_*mes 5

您的视图不知道来自何处IngredientRecipe来自何处,您需要添加对这些类型所在的名称空间的引用,并添加@using XXX.Models到视图顶部

@model XXX.ViewModels.NavigationViewModel
@using XXX.Models
...
@foreach (Ingredient ingredient in Model.Ingredients)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

在旁注中,您似乎有一个半熟的视图模型实现。在您NavigationViewModel所引用的域模型中。通常建议通过视图模型公开的任何东西实际上都是视图模型本身。因此,在您的情况下,我将介绍几个新的视图模型来表示一个Ingredient/ Recipe

public class IngredientViewModel
{
    ...
}

public class RecipeViewModel
{
    ...
}

public class NavigationViewModel
{
    public IEnumerable<IngredientViewModel> Ingredients { get; set; }
    public IEnumerable<RecipeViewModel> Recipes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这些将在下创建,XXX.ViewModels这意味着您的视图看起来像

@using XXX.ViewModels
@model NavigationViewModel

...
Run Code Online (Sandbox Code Playgroud)