ASP.Net MVC 4传递到字典的模型项的类型为“System.Collections.Generic.List`1 [System.Int32]”
我想填充的国家名单(数据来自国家表在DB)在下拉列表中。我得到以下错误:
The model item passed into the dictionary is of type
System.Collections.Generic.List`1[System.Int32]', but this dictionary requires a model item of type 'BIReport.Models.Country'.
我新的ASP.Net MVC,我不明白的错误。我觉得Index方法返回的内容与我在View中使用的模型不匹配。
型号::
namespace BIReport.Models
{
public partial class Country
{
public int Country_ID { get; set; }
public string Country_Name { get; set; }
public string Country_Code { get; set; }
public string Country_Acronym { get; set; }
}
}
控制器::
public class HomeController : Controller
{
private CorpCostEntities _context;
public HomeController()
{
_context = new CorpCostEntities();
}
//
// GET: /Home/
public ActionResult Index()
{
var countries = _context.Countries.Select(arg => arg.Country_ID).ToList();
ViewData["Country_ID"] = new SelectList(countries);
return View(countries);
}
}
查看::
@model BIReport.Models.Country
<label>
Country @Html.DropDownListFor(model => model.Country_ID, ViewData["Country_ID"] as SelectList)
</label>
我要去哪里错了?
我有一个国家的表,其中有很多其他列,然后Country_Name。这就是为什么我把国家当作模范。不知道这是否是错误的。顺便说一句,在你的情况下,视图代码的外观如何?对不起,问这个。 – shaz
所以你的观点应该是由多个国家组成的表格? –
我不确定如何在MVC上下文中说它,但我试图在我的索引页上显示一个带有国家/地区列表的下拉列表。所以我有一个名为Country的表,其中 – shaz