I cannot link dropdownlist to my model in the view. I have the error message: The ViewData item that has the key 'title' is of type 'System.String' but must be of type 'IEnumerable with the following code:
public class FareCustomer
{
[Required]
public int title { get; set; }
My controller:
List<SelectListItem> titleList = new List<SelectListItem>();
titleList.Add(new SelectListItem { Text = "Non renseigné", Value = "0" });
titleList.Add(new SelectListItem { Text = "Monsieur", Value = "1" });
titleList.Add(new SelectListItem { Text = "Madame", Value = "2" });
titleList.Add(new SelectListItem { Text = "Mademoiselle", Value = "3" });
ViewBag.title = titleList;
//Create a new customer
FareCustomer fareCustomer = new FareCustomer();
fareCustomer.title = 1;
return View("CreateAccount", fareCustomer);
My view 'CreateAccount'
@model PocFareWebNet.Models.FareCustomer
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
@Html.DropDownList("title")
<input type="submit" value="Save" />
</fieldset>
}
I tried to apply what is described here: Using the DropDownList Helper with ASP.NET MVC but obviously I missed something.