0

I cannot understand why I have this error because what I'm trying to do should be fairly simple:

The model item passed into the dictionary is of type 'WebApplication1.Models.Location', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[WebApplication1.Models.Location]'.

View code Location_List.cshtml

@model List<WebApplication1.Models.Location>

@{
    Layout = "~/Views/Shared/_LayoutAdminPanel.cshtml";
}

<div class="table-responsive">
    <table class="table">
        <thead class="thead-inverse">
            <tr>
                <th>Location ID</th>
                <th>Location Name</th>
                <th>Photo Path</th>
                <th>Details</th>
                <th>City ID</th>
                <th>Modify</th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Count; i++)
            {

                    <tr>
                        <th scope="row">Location00_@Model[i].LocationID</th>
                        <td>@Model[i].LocationName</td>
                        <td>@Model[i].PhotoPath</td>
                        <td>@Model[i].Details</td>
                        <td>@Model[i].CityID</td>
                        <td><a href="/Admin/Location_Edit?ID=@i">Edit</a></td>
                    </tr>

            }

Controller Code AdminController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace MySafar.Controllers
{
    public class AdminController : Controller
    {

      public ActionResult Location_New()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Location_List()
        {
            Location l = new Location();
            l.LocationID = Convert.ToInt32(Request.Form["LocationID"]);
            l.LocationName = Request.Form["LocationName"];
            l.PhotoPath = Request.Form["PhotoPath"];
            l.Details = Request.Form["Details"];
            l.CityID = Convert.ToInt32(Request.Form["CityID"]);

            if (Session["Location"]==null)
            {
                Session["Location"] = new List<Location>();
            }

            List<Location> loc = (List<Location>)Session["Location"];
            loc.Add(l);
            return View("Location_List", l);
        }
}

I was check whole code 4-5 times i cannot understand where is the actual problem... any solution? please....

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Dev Dhande
  • 39
  • 1
  • 6

2 Answers2

2

Based on your error, it sounds like whatever model you specified in the cshtml file, is expecting a LIST of WebApplication1.Models.Location types, but you're only passing a single WebApplication1.Models.Location into it, as it clear by

return View("Location_List", l);

Either change the model that the cshtml file is expecting, to be a single location, or pass in a List of locations to the model. In fact, I think just changing that above line to this

return View("Location_List", loc);

will fix your issue. Cheers!

rickjerrity
  • 804
  • 1
  • 9
  • 15
0

You try to send l variable to the Location_List view:

return View("Location_List", l);

The l is an instance of the Location type. But in the Location_List.cshtml file you define that the model type is List. I mean you need to send loc variable to the view instead of l.

Alexander
  • 4,420
  • 7
  • 27
  • 42