简体   繁体   English

ASP.Net MVC“传入字典的模型项是'System.Collections.Generic.List'类型”

[英]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. 我是MVC的新手,请帮我解决这个问题。 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'." 我得到的错误是“传入字典的模型项类型为'System.Collections.Generic.List`1 [LearningMVCBLL.CityMaster]',但此字典需要类型为'LearningMVC.Models.User'的模型项“。

I am using LINQ to fetch the data from the database. 我正在使用LINQ从数据库中获取数据。 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替换

@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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递到字典中的模型项在ASP.net MVC中的类型为“ System.Collections.Generic.List…” - The model item passed into the dictionary is of type 'System.Collections.Generic.List… in ASP.net MVC 在构建 ASP.NET Core Web 应用程序时,我得到“传递到字典中的模型项的类型为‘System.Collections.Generic.List`1” - While building ASP.NET Core web app I get "The model item passed into the dictionary is of type 'System.Collections.Generic.List`1" 传递到字典中的模型项的类型为“ System.Collections.Generic.List” - The model item passed into the dictionary is of type 'System.Collections.Generic.List` 传递到字典中的模型项的类型为&#39;System.Collections.Generic.List,但是此字典需要类型为的模型项 - model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type 传递到字典中的模型项的类型为&#39;System.Collections.Generic.List`1 [System.String] - The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String] 异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“ System.Collections.Generic.List”。 - Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List` 传递到 ViewDataDictionary 的 model 项的类型为“System.Collections.Generic.List” - The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List` 传递到字典中的模型项的类型为&#39;System.Collections.Generic.List`1 [TipoDeCanal]&#39;, - The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[TipoDeCanal]', 字典需要System.Collections.Generic.List类型的模型项 - Dictionary requires a model item of type System.Collections.Generic.List 传入字典的 model 项的类型为 'System.Collections.Generic.List,如何处理? - The model item passed into the dictionary is of type 'System.Collections.Generic.List, how to handel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM