“传入字典的模型项的类型是'System.Data.Entity.DynamicProxies但是......'MVC问题

时间:2017-01-19 17:44:35

标签: c# asp.net-mvc

我真的在网上搜索过,我只是堆叠在这个问题上..请帮助:((与MVC新) 当我试图运行一个特定的页面时,我收到消息:

  

System.InvalidOperationException:传递给字典的模型项的类型为'System.Data.Entity.DynamicProxies.Category_B85D1A3F838C0192719C683B1095328267C570F7A660F0D',但此字典需要类型为'Leeoriyas.Models.Category'的模型项。

我的控制器“storecontroller”(相关部分)

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Leeoriyas.Models;

    namespace Leeoriyas.Controllers

public class StoreController : Controller
    {
        LeeoriyasFPEntities storeDB = new LeeoriyasFPEntities();
        // GET: Store


     public ActionResult Index()
            {
                return View(storeDB.Category.ToList());
            }
 public ActionResult Browse(string Category)
        {
            var categoryModel = storeDB.Category.Include("ArtS").Single(c => c.Name == Category);
                return View(categoryModel);
        }

我的观点(浏览)

@model Leeoriyas.Models.Category


@{
    ViewBag.Title = "Browse";
}

<h2>Browsing category: @Model.Name</h2>

<ul>
    @foreach (var product in Model.ArtS)
    {
        <li>
            @product.Title
        </li>
    }
</ul>

谢谢! :(

1 个答案:

答案 0 :(得分:0)

您没有发送预期视图。您指定类型Leeoriyas.Models.Category,因此请确保传递此类对象。你应该传递这样的东西:

 public ActionResult Browse(string category)
        {
            Leeoriyas.Models.Category categoryModel = storeDB.Category.Include("ArtS").Single(c => c.Name == category);
                return View(categoryModel);
        }

确保此行storeDB.Category.Include("ArtS").Single(c => c.Name == category);的返回类型为Category