Satrio Dwi Huripto
Satrio Dwi Huripto

Reputation: 35

can't submit dropdownlistfor

My model :

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

My Controller:

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();
    }

and my View :

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

then I got an error like this:

Unable to cast object of type 'System.Collections.Generic.List`1[System.Web.WebPages.Html.SelectListItem]' to type 'System.String'

What I must to do? Please Help. Thanks before

Upvotes: 1

Views: 88

Answers (1)

Saranga
Saranga

Reputation: 3228

Try changing the type of AO from IEnumerable<SelectListItem> to string.

public string AO { get; set; }

Thanks!

Upvotes: 1

Related Questions