简体   繁体   中英

ASP.Net MVC “The model item passed into the dictionary is of type 'System.Collections.Generic.List”

View

@using LearningMVCBLL;
@model LearningMVC.Models.User

@{
    ViewBag.Title = "CityDetails";
}

<h2>CityDetails</h2>
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Value</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>City Name</td>
            <td>@foreach (CityMaster city in @Model.GetCityDetails())
                {
                @city.City_Name
                }
            </td>
        </tr>
        @* <tr>
      <td></td>
      <td></td>
    </tr>*@
    </tbody>
</table>

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LearningMVC.Models;
using LearningMVCBLL;

namespace LearningMVC.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /Index/

        public ActionResult CityDetails()
        {
            User User = new User();
            List<CityMaster> city = new List<CityMaster>();
            city = User.GetCityDetails();
            return View(city);
        }

    }
}

Model

using LearningMVCBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LearningMVC.Models
{
    public class User
    {
        LearningMVCDataContext _dbContext = new LearningMVCDataContext();
        public List<CityMaster> GetCityDetails()
        {
            List<CityMaster> city = _dbContext.CityMasters.Where(c => c.City_ID == 2).ToList();
            return city;
        }
    }

}

I am new to MVC please help me in resolving this problem. Error that I am getting is "The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[LearningMVCBLL.CityMaster]', but this dictionary requires a model item of type 'LearningMVC.Models.User'."

I am using LINQ to fetch the data from the database. Please explain me what I am doing wrong or I am completely wrong.

In your action method you have this code...

public ActionResult CityDetails() 
{ 
    User User = new User(); 
     List<CityMaster> city = new List<CityMaster>(); 
     city = User.GetCityDetails(); 
     return View (city);
 }

You need to change it to this....

public ActionResult CityDetails() 
{
     User user = new User();
      return View(user);
}

You may use Leo answer, if you want have User in view. Or you may change view: Replace model

@model LearningMVC.Models.User

To

@model List<CityMaster>

And in foreach replace

@foreach (CityMaster city in @Model.GetCityDetails())

To

@foreach (CityMaster city in Model)

Although you already have an answer you'd be better of constructing the data you need in the controller.

Example:

public class CityDetailsModel
{
    public User User { get; set; }
    public IEnumerable<Cities> { get; set; }
}


public class UserController : Controller
{
    //
    // GET: /Index/
    public ActionResult CityDetails()
    {
        CityDetailsModel model = new CityDetailsModel();
        model.User = new User();
        model.Cities = GetCityDetails();
        return View(model);
    }
}

View:

@using LearningMVCBLL;
@model LearningMVC.Models.CityDetailsModel
@{
    ViewBag.Title = "CityDetails";
}
<h2>CityDetails</h2>
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Value</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>City Name</td>
            <td>
            @foreach (CityMaster city in @Model.Cities)
            {
                @city.City_Name
            }
            </td>
        </tr>
        @* <tr>
      <td></td>
      <td></td>
    </tr>*@
    </tbody>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM