[英]Create a Dynamic List using Entity Framework in MVC Razor
I would like to make a list populate with vehicle makes using a stored procedure and Entity Framework but when it I get the following error: 我想使用存储过程和实体框架使用车辆制造商来填充列表,但是当出现以下错误时:
The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[VehicleInfo.Models.tblVehicle]'. 传递到词典中的模型项的类型为'System.Collections.Generic.List
1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [VehicleInfo.Models.tblVehicle ]”。
Model 模型
public partial class tblVehicle
{
public string BrandName{ get; set; }
}
Controller 调节器
public ActionResult Index()
{
VehicleDBContext db = new VehicleDBContext();
var brandnames = db.prcGetMakes().ToList();
return View(brandnames);
}
View 视图
@model IEnumerable<VehicleInfo.Models.tblVehicle>
<ul id="Makes">
@foreach (var item in Model)
{
<li>@item.BrandName.Distinct()</li>
}
</ul>
Stored Procedure 储存程序
CREATE PROCEDURE [dbo].[prcGetMakes]
AS
BEGIN
SET NOCOUNT ON;
SELECT DISTINCT
UPPER(BrandName) BrandName
FROM
tblVehicle
ORDER BY
BrandName
END
There must be something amazingly obvious that I am missing but I can't seem to work it out. 肯定有一些我很想念的东西,但是我似乎无法解决。
Please Help! 请帮忙!
From the error message, It looks like the expression db.prcGetMakes().ToList()
in your action method returns a list of strings and you are passing that to the view. 从错误消息中,您的action方法中的表达式
db.prcGetMakes().ToList()
看起来返回了一个字符串列表,并将其传递给视图。 But your view is strongly typed to a list of tblVehicle
objects, Hence getting the error message about the type mismatch. 但是,您的视图被强类型
tblVehicle
对象的列表,因此会收到有关类型不匹配的错误消息。
Solution is to make both types match. 解决方案是使两种类型匹配。 You may update your view to be strongly typed list of
string
type 您可以将视图更新为
string
类型的强类型列表
@model IEnumerable<string>
<ul id="Makes">
@foreach (var item in Model)
{
<li>@item</li>
}
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.