【问题标题】:ASP.Net MVC "The model item passed into the dictionary is of type 'System.Collections.Generic.List"ASP.Net MVC“传入字典的模型项的类型为'System.Collections.Generic.List”
【发布时间】:2014-01-02 17:51:42
【问题描述】:

查看

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

控制器

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);
        }

    }
}

型号

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;
        }
    }

}

我是 MVC 新手,请帮我解决这个问题。我得到的错误是“传递到字典中的模型项的类型为'System.Collections.Generic.List`1[LearningMVCBLL.CityMaster]',但该字典需要'LearningMVC.Models.User'类型的模型项。”

我正在使用 LINQ 从数据库中获取数据。请解释一下我做错了什么或者我完全错了。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    虽然您已经有了答案,但最好在控制器中构造您需要的数据。

    例子:

    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);
        }
    }
    

    查看:

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

    【讨论】:

    • 感谢 Sivermind 我认为您的回答有助于我创建更强类型的视图。我对吗? @silvermind
    • @ManojSethi 确实如此;) 它可以帮助您使用方法防止视图混乱。一个额外的优势是调试。从你的视图调试到方法并不是真的那么棒。不,您的优势在于,当您收到有关逻辑的异常时,它会发生在控制器中。
    • 非常感谢我是 MVC 模式的新手。感谢您详细解释。
    【解决方案2】:

    在您的操作方法中,您有此代码...

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

    你需要把它改成这个....

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

    【讨论】:

      【解决方案3】:

      如果您想查看用户,您可以使用 Leo 答案。或者您可以更改视图: 更换型号

      @model LearningMVC.Models.User
      

      @model List<CityMaster>
      

      并在foreach 中替换

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

      @foreach (CityMaster city in Model)
      

      【讨论】:

      • 工作完美感谢您的帮助
      • @ManojSethi,我很乐意提供帮助。顺便说一句,如果您想要并使用此答案,为什么您将不同的答案标记为最佳?
      • 我实际上希望在视图中返回用户,但现在这两个答案都对我有用,所以我标记了 Leo 的答案,因为它给了我想要实现的目标。我希望 StackOverflow 允许多个答案标记为正确。
      猜你喜欢
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多