I have this code in my Controller:
[HttpGet]
public ActionResult Create()
{
List<SelectListItem> objResult = new List<SelectListItem>();
Models.Countries country = new Models.Countries();
DataTable result = country.GetAllCountries();
foreach(DataRow row in result.Rows)
{
objResult.Add(new SelectListItem
{
Text = row["country"].ToString(),
Value = row["id"].ToString()
});
}
ViewBag.country = objResult;
return View();
}
and then within my view I have:
@model Project.Models.CountryViewModel
@Html.DropDownList("country", (IEnumerable<SelectListItem>)ViewBag.country, "Select")
And then I render this partial view within another view:
@model Project.Models.Register
@using (Ajax.BeginForm("Register", "Register", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "complete"}))
{
<div id="complete">
@Html.Partial("~/Views/Shared/Countries.cshtml, new Project.Models.CountryViewModel()")
</div>
}
Within CountryViewModel I have:
public int CountryID { set; get; }
public string country { set; get; }
However I get this error:
Additional information: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'country'.
Does anyone know what I am doing wrong?