ASP.Net MVC 4
我正在尝试在下拉列表中填充国家列表(数据库中国家表中的数据)。我收到以下错误:
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 方法返回的与我在视图中使用的模型不匹配。
模型::
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>
我哪里错了?