[英]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.