无法提交dropdownlistfor

时间:2014-07-15 08:41:16

标签: asp.net-mvc-4 html.dropdownlistfor

我的模特:

    [Display(Name = "AO")]
    [Required]
    [StringLength(20)]
    public IEnumerable<SelectListItem> AO { get; set; }

我的控制器:

public ActionResult ExpCreate()

 {
        List<SelectListItem> list = new List<SelectListItem>();
        list.Add(new SelectListItem { Text = "Pilih", Value = " " });
        var cat = (from c in db.AOs select c.UserID).ToArray();
        for (int i = 0; i < cat.Length; i++)
        {
            list.Add(new SelectListItem { Text = cat[i], Value = cat[i].ToString() });
        }
        ViewData["AODATA"] = list;
        ViewData["AODATAs"] = ViewData["AODATA"].ToString();
        return View();
    }

和我的观点:

Html.DropDownList("AO", (IEnumerable<SelectListItem>)ViewData["AODATA"])

然后我收到了这样的错误:

无法将类型为'System.Collections.Generic.List`1 [System.Web.WebPages.Html.SelectListItem]'的对象强制转换为'System.String'

我必须做什么?请帮忙。先谢谢

1 个答案:

答案 0 :(得分:1)

尝试将AO的类型从IEnumerable<SelectListItem>更改为string

public string AO { get; set; }

谢谢!