I have a Controller and a View and I would like to populate a dropdownlist.
My Controller looks like this:
namespace AAA.Controllers
{
public class BBBController: Controller
{
[HttpPost]
public ActionResult Sort_Order()
{
List<SelectListItem> liSort = new List<SelectListItem>();
liSort.Add(new SelectListItem() { Text = "--Select--", Value = "0", Selected = true });
liSort.Add(new SelectListItem() { Text = "ASC", Value = "1" });
liSort.Add(new SelectListItem() { Text = "DESC", Value = "2" });
ViewData["ddSort"] = liSort;
return View();
}
}
}
My View looks like this:
@model IEnumerable<AAA.Models.xyz>
@{
ViewBag.Title = "List";
}
<br />
<h2>List</h2>
<table class="table">
<tr>
<th>
Sort order
</th>
</tr>
<tr>
<td>
@using (Html.BeginForm("Sort_Order", "BBB", FormMethod.Post))
{
@Html.DropDownList("Sort", ViewData["ddSort"] as IEnumerable<SelectListItem>, new { @class = "form-control", @style = "width:200px; height:30px" })
}
</td>
</tr>
</table>
When I run this I get a error:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Sort'.*
Can someone help me and tell me what I am doing wrong?