0

I am trying to bind a Telerik DropDownList.

View Code:

<div>@( Html.Telerik().DropDownList()
         .Name("ddlCounty")
         .HtmlAttributes(new { style = "width:200px;" })
         .SelectedIndex(0)
         .BindTo(new SelectList((IEnumerable<MvcNew.Models.tbl_Country>)ViewData["ListCountries"], "Value", "Text"))                                        )
  </div> 

Controller Code:

List<SelectListItem> lst_Country = new List<SelectListItem>();
var Countries = (from m in DBContext.tbl_Countries
               select new SelectListItem{ Text = m.Country__Name.ToString(), Value = m.Country_ID.ToString() });
ViewBag.ListCountries = new SelectList(Countries);
 return View();

I am getting the below error
Unable to cast object of type 'System.Web.Mvc.SelectList' to type 'System.Collections.Generic.IEnumerable`1[MvcNew.Models.tbl_Country]'.

4

1 回答 1

0

我已经更改了这样的代码并且它有效

       var clientIDs = DBContext.tbl_Countries  
        List<SelectListItem> items = new List<SelectListItem>();


        foreach (var t in clientIDs)
        {
            SelectListItem s = new SelectListItem();           
            s.Text = t.Country__Name.ToString();
            s.Value = t.Country__Name.ToString();
            items.Add(s);
        }

        ViewBag.ListCountries = items;
于 2013-04-02T07:21:35.117 回答