Why Telerik's DropDownList doesn't have any values when using ajax databinding?
asp.net, c#, telerik, telerik-mvc
Solution
Hmm, your doing a strange thing : You pass a `Dictionary<string, string>` to your select list, and asserts that the valueField is "ProductId", and TextField is "ProductName".
Your dictionary has not such properties... Strongly typing is good.
So you would need a class Product (or whatever)
public class Product {
public int ProductId {get;set;}
public string ProductName {get;set;}
}
and use it, even for test purpose
public ActionResult _AjaxLoading(string text) {
var products= new List<Product> {
new Product{ProductId = 1, ProductName="b"}
};
return new JsonResult { Data = new { Text = "Abc", Value = "123", Products= new SelectList(products, "ProductID", "ProductName") } };
}
EDIT :
By the way, if you want "Abc" and "123" in SelectList, this is not the right way to do, look at @Gaby's answer in your previous post https://stackoverflow.com/a/10500876/961526
EDIT 2 :
let's try again
so firt, some of my usual extension classes (I limited them, they used to be more generic, but... anyway)
public static class ComboExtensions
{
public static IEnumerable<SelectListItem> ToSelectListItem<T>(this IEnumerable<T> enumerable,
Func<T, string> text,
Func<T, int> value)
{
return enumerable.Select(item => new SelectListItem
{
Text = text(item).ToString(),
Value = value(item).ToString(),
}).AsEnumerable();
}
public static IEnumerable<SelectListItem> WithDefaultValue(this IEnumerable<SelectListItem> selectListItems, int defaultValue = 0, string chooseText = "choose")
{
IList<SelectListItem> items = selectListItems.ToList();
items.Insert(0, new SelectListItem {Value = defaultValue.ToString(), Text = chooseText});
then
public ActionResult _AjaxLoading(string text) {
var products = new List<Product>
{
new Product {ProductId = 1, ProductName = "b"}
}.ToSelectListItem(m => m.ProductName, m => m.ProductId)
.WithDefaultValue(1, "abc");
return new JsonResult { Data = products } };
}
Problem
I'm using Asp.Net with Razor syntax and latest Telerik components. Unfortunately when I click on dropdown box I see nothing in it but VS debugger shows me that I executed _AjaxLoaiding method. How can I solve this mystery (i.e. loading data into DropDownList)? This is part of my Controller: ``` public ActionResult _AjaxLoading(string text) { var product = new Dictionary<string, string>(); product.Add("a","b"); return new JsonResult { Data = new { Text = "Abc", Value = "123", Produtcs = new SelectList(product, "ProductID", "ProductName") } }; } ``` This is part of my View: ``` @{Html.Telerik().DropDownList() .Name("documentType").Enable(false) .HtmlAttributes(new { style = "width:250px;" }) .DataBinding(binding => binding.Ajax().Select("_AjaxLoading", "Applicants")) .Render(); } ```