【发布时间】:2014-08-08 15:09:27
【问题描述】:
我刚刚使用实体框架代码优先方法创建了一个数据库。
这是我的模态类
public class MobileProduct
{
[Key]
public int p_id { get; set; }
public string p_name { get; set; }
public string p_description { get; set; }
public string p_price { get; set; }
}
这是我的 DbContext 派生类
public class Entities : DbContext
{
public DbSet<MobileProduct> Mobiles { get; set; }
}
我刚刚创建了一个查询来将此数据传递给操作方法的视图
Entities db = new Entities();
public ActionResult Mobiles()
{
var q = from n in db.Mobiles
select n;
return View(q);
}
这是我的观点
@model HomeShopping.Models.MobileProduct
@{
ViewBag.Title = "Mobiles";
}
<h2>Mobiles</h2>
访问视图时出现错误:
传入字典的模型项的类型为“System.Data.Entity.Infrastructure.DbQuery`1[HomeShopping.Models.MobileProduct]”,但此字典需要“HomeShopping.Models.MobileProduct”类型的模型项.
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.InvalidOperationException:传入字典的模型项的类型为“System.Data.Entity.Infrastructure.DbQuery`1[HomeShopping.Models.MobileProduct]”,但此字典需要类型的模型项'HomeShopping.Models.MobileProduct'。
【问题讨论】:
-
问题是什么?
标签: c# asp.net-mvc entity-framework asp.net-mvc-4 localdb