[英]Passing entity framework query to view error
I just created a database with Entity Framework Code first Approach. 我刚刚使用实体框架代码优先方法创建了一个数据库。
Here is my modal class 这是我的模态课
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; }
}
Here is my DbContext derived class 这是我的DbContext派生类
public class Entities : DbContext
{
public DbSet<MobileProduct> Mobiles { get; set; }
}
I just created a query to pass this data t view on an action method 我刚刚创建了一个查询,以通过操作方法传递此数据视图
Entities db = new Entities();
public ActionResult Mobiles()
{
var q = from n in db.Mobiles
select n;
return View(q);
}
Here is my View 这是我的看法
@model HomeShopping.Models.MobileProduct
@{
ViewBag.Title = "Mobiles";
}
<h2>Mobiles</h2>
I got an error when accessing the view: 访问视图时出现错误 :
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[HomeShopping.Models.MobileProduct]', but this dictionary requires a model item of type 'HomeShopping.Models.MobileProduct'.
传递到字典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery`1 [HomeShopping.Models.MobileProduct]',但是此字典需要类型为'HomeShopping.Models.MobileProduct'的模型项。
Description: An unhandled exception occurred during the execution of the current web request.
说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code.
请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[HomeShopping.Models.MobileProduct]', but this dictionary requires a model item of type 'HomeShopping.Models.MobileProduct'.
异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“ System.Data.Entity.Infrastructure.DbQuery`1 [HomeShopping.Models.MobileProduct]”,但是此字典需要模型项为“ HomeShopping”。 Models.MobileProduct”。
I presume you want to send a list of MobileProduct to the view? 我假设您要向视图发送MobileProduct列表? If so;
如果是这样的话; change your model to
将您的模型更改为
@model IEnumerable<HomeShopping.Models.MobileProduct>
@{
ViewBag.Title = "Mobiles";
}
<h2>Mobiles</h2>
Currently your model only expects one instance of type MobileProduct whereas your Mobiles action is creating a list of MobileProduct. 当前,您的模型仅需要一个MobileProduct类型的实例,而您的Mobiles操作正在创建MobileProduct的列表。 You will also need to evaluate the query in the action by performing a ToList() on it.
您还需要通过对操作执行ToList()来评估该操作中的查询。
public ActionResult Mobiles()
{
var q = from n in db.Mobiles
select n;
return View(q.ToList());
}
You aren't realizing your query so you're sending the query itself, not the results of the query, to the View. 您没有实现查询,因此将查询本身(而不是查询结果)发送到视图。
var q = (from n in db.Mobiles
select n).ToList();
return View(q);
As daveL posted, you also need to change the definition of the model in your view. 正如daveL发布的那样,您还需要在视图中更改模型的定义。
@model IEnumerable<HomeShopping.Models.MobileProduct>
Haven't tested, but try defining your model as: 尚未测试,但尝试将模型定义为:
@model IQuerable<HomeShopping.Models.MobileProduct>
If that doesn't work, then perhaps: 如果那不起作用,那么也许:
@model IEnunerable<HomeShopping.Models.MobileProduct>
...with your object returned as: ...对象返回为:
View(q.ToList());
you are saving a query to q, not the evaluation of the query. 您将查询保存到q,而不是查询的评估。 You need a ToList() if you expect multiple results.
如果期望多个结果,则需要一个ToList()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.