0

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.List1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[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!

2
  • 1
    What is the type of the result of db.prcGetMakes().ToList() ? Is it a list of string ? then the error is self explanatory. Your view is strongly typed to a list of tblVehicle. With the current view code, you basically need to return a list of tblVehicle objects. Commented Apr 18, 2017 at 20:52
  • Thank-you so much :) Commented Apr 19, 2017 at 18:34

1 Answer 1

3

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. But your view is strongly typed to a list of tblVehicle objects, Hence getting the error message about the type mismatch.

Solution is to make both types match. You may update your view to be strongly typed list of string type

@model IEnumerable<string>

<ul id="Makes">
   @foreach (var item in Model)
   {
      <li>@item</li>
   }
</ul>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.