查看ASP.Net MVC 「傳遞到字典的模型項的類型爲「System.Collections.Generic.List」
@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從數據庫中獲取數據。請解釋我做錯了什麼,或者我完全錯了。
幫助 –
我很高興謝謝!!!! – Leo