0

Im creating a getting a Location type of application

I am receiving this error

Model item passed into the dictionary is of type 'System.Collections.Generic.List[CA1API.Models.Weather]', but this requires model item type 'System.Collections.Generic.IEnumerable [CA1API.Models.Location]'. 

I have created my ViewModel that looks like this namespace CA1API.Models

{
    public enum County
    {
        Ireland,
        England,
        Iceland
    }

    public class Location
    {
        public int LocationID { get; set; }
        [Display(Name = "Location")]
        public County LocationName { get; set; }
        public double Lat { get; set; }
        public double Lon { get; set; }
        public int WeatherID { get; set; }
        public List<Weather> Weathers { get; set; }

    }
}

my controller looks like this

public class HomeController : Controller
    {
        private WeatherDb db = new WeatherDb();
        public ActionResult Index()
        {

            return View(db.Weathers.ToList());
        }
    }

my view looks like this

@model IEnumerable<CA1API.Models.Location>
<div class="row">
    <div class="col-md-2">
        <div class="panel panel-success">
            <div class="panel-heading">County</div>
            <div>
                <div class="panel-body" style="overflow-x:hidden; height:300px;">
                    @foreach (var item in Model)
                    {
                        <p>modelItem=>item.County</p>
                    }
                </div>
            </div>
        </div>
    </div>
  • Can you add the top of the View Index? the line that starts with `@model ... `? – adricadar Mar 12 '15 at 12:06
  • 2
    You view has `@model IEnumerable` - but your passing `IEnumerable`. Change the view to `@model IEnumerable` (Not exactly sure what the view should be showing, but the types must match) –  Mar 12 '15 at 12:06

2 Answers2

0

Your Index View use the @model IEnumerable<CA1API.Models.Location> as a Model and you try to pass an @model List<CA1API.Models.Weather> to the View.

Because the model you try to pass don't inherit or it's not the model that's expecting to be received it's throwing an exception.

Model item passed into the dictionary is of type 'System.Collections.Generic.List[CA1API.Models.Weather]', but this requires model item type 'System.Collections.Generic.IEnumerable [CA1API.Models.Location]'.

adricadar
  • 9,971
  • 5
  • 33
  • 46
0

The error is pretty clear. You're passing a List<Weather> where the view expects IEnumerable<Location>.

You probably meant return View(db.Locations.ToList()); instead of db.Weathers.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272