![](/img/trans.png)
[英]How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?
[英]Create a Dynamic List using Entity Framework in MVC Razor
我想使用存儲過程和實體框架使用車輛制造商來填充列表,但是當出現以下錯誤時:
傳遞到詞典中的模型項的類型為'System.Collections.Generic.List 1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [VehicleInfo.Models.tblVehicle ]”。
模型
public partial class tblVehicle
{
public string BrandName{ get; set; }
}
調節器
public ActionResult Index()
{
VehicleDBContext db = new VehicleDBContext();
var brandnames = db.prcGetMakes().ToList();
return View(brandnames);
}
視圖
@model IEnumerable<VehicleInfo.Models.tblVehicle>
<ul id="Makes">
@foreach (var item in Model)
{
<li>@item.BrandName.Distinct()</li>
}
</ul>
儲存程序
CREATE PROCEDURE [dbo].[prcGetMakes]
AS
BEGIN
SET NOCOUNT ON;
SELECT DISTINCT
UPPER(BrandName) BrandName
FROM
tblVehicle
ORDER BY
BrandName
END
肯定有一些我很想念的東西,但是我似乎無法解決。
請幫忙!
從錯誤消息中,您的action方法中的表達式db.prcGetMakes().ToList()
看起來返回了一個字符串列表,並將其傳遞給視圖。 但是,您的視圖被強類型tblVehicle
對象的列表,因此會收到有關類型不匹配的錯誤消息。
解決方案是使兩種類型匹配。 您可以將視圖更新為string
類型的強類型列表
@model IEnumerable<string>
<ul id="Makes">
@foreach (var item in Model)
{
<li>@item</li>
}
</ul>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.