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