The Dropdownlist helper throw this error when it cannot find a list of select options. The Dropdownlist constructor overload you used looks for a ViewData dictionary key with the same name as the Dropdownlist when the option list passed to the constructor is null. You named the Dropdownlist "TenDonVi". There is no ViewData dictionary item named "TenDonVi" and why you see the error message "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'TenDonVi'."
The first thing you should do is set a break point where the ViewBag.DonVis is populated. I'm guessing ViewBag.DonVis is null. You'll need to figure out why it's null. Typically on the forum we see developers will populate the options in the GET action but not POST. You must populate the options in GET and the POST.
The follow code examples illustrate how to produce the error as well as successfully populate the options..
public ActionResult Index()
{
//Working example
//List<int> TenDonVi = new List<int>() { 1, 2, 3, 4, 5 };
//ViewBag.TenDonVi = new SelectList(TenDonVi);
//Error
ViewBag.TenDonVi = null;
//Empty dropdown
//List<int> TenDonVi = new List<int>();
//ViewBag.TenDonVi = new SelectList(TenDonVi);
return View();
}
Markup
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
@Html.DropDownList("TenDonVi", ViewBag.DonVis as SelectList)
</div>