简体   繁体   English

在MVC Razor中使用实体框架创建动态列表

[英]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.

相关问题 如何使用带有剃刀视图的实体框架(.edmx模型)为MVC4或MVC 5创建局部视图? - How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views? 从访客创建客户 - 使用 MVC3、C#、实体框架、Razor 视图、SQL 服务器 - Create a Customer from a Guest - using MVC3, C#, Entity Framework, Razor Views, SQL Server 使用实体框架脚手架和 Razor 对下拉列表进行 MVC 过滤 - MVC filter on dropdownlist using entity framework scaffolding and Razor 从 Razor 更新列表使用 Entity Framework Core 查看 - Update list from Razor View using Entity Framework Core 使用Dynamic Linq Core和Entity Framework 2.0创建包含列表的对象 - Create object containing a list using Dynamic Linq Core with Entity Framework 2.0 在“实体框架剃刀”列表上的不同查询项 - Distinct items of query on a list “entity framework Razor” 将Entity Framework与“动态”实体一起使用? - Using Entity Framework with “dynamic” entity? 如何使用ASP.NET MVC剃须刀中的实体框架检查数据库中是否存在记录? - How to Check if record exist in database using Entity Framework in ASP.NET MVC razor? 在使用实体框架的MVC4 Intranet应用程序中无法在Razor语法中使用HtmlHelper - Unable to Use an HtmlHelper in Razor syntax in MVC4 Intranet App using Entity Framework 如何使用实体框架管理的类的属性创建动态过滤器? - How to create a dynamic filter using the attributes of a class managed by the Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM