【问题标题】:Bind Telerik DDL绑定 Telerik DDL
【发布时间】:2013-03-22 13:43:02
【问题描述】:

我正在尝试绑定 Telerik DropDownList。

查看代码:

<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> 

控制器代码:

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

我收到以下错误
无法将“System.Web.Mvc.SelectList”类型的对象转换为“System.Collections.Generic.IEnumerable`1[MvcNew.Models.tbl_Country]” .

【问题讨论】:

    标签: drop-down-menu telerik


    【解决方案1】:

    我已经更改了这样的代码并且可以正常工作

           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-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2012-07-25
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多