我无法将下拉列表链接到视图中的模型。我收到错误消息:具有键“title”的 ViewData 项的类型为“System.String”,但必须为“IEnumerable”类型,代码如下:
public class FareCustomer
{
[Required]
public int title { get; set; }
我的控制器:
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);
我的观点'CreateAccount'
@model PocFareWebNet.Models.FareCustomer
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
@Html.DropDownList("title")
<input type="submit" value="Save" />
</fieldset>
}
我尝试应用这里描述的内容:Using the DropDownList Helper with ASP.NET MVC 但显然我错过了一些东西。