Why does Telerik DropDownList have no values โ€‹โ€‹when using ajax data binding?

I am using Asp.Net with Razor syntax and the latest Telerik components. Unfortunately, when I click on the drop-down list, I see nothing in it, but the VS debugger shows me that I executed the _AjaxLoaiding method. How can I solve this mystery (i.e. upload data to 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(); } 
+4
source share
1 answer

Hmm, your strange thing: you pass Dictionary<string, string> to your select list and claim that the Field value is "ProductId" and the TextField is "ProductName".

There are no such properties in your dictionary ... Good text customization.

So you need the Product class (or something else)

 public class Product { public int ProductId {get;set;} public string ProductName {get;set;} } 

and use it even for testing purposes

 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 the wrong way to do it, look at @Gaby's answer in your previous post to fooobar.com/questions/1411336 / ...

EDIT 2:

try again

so firt, some of my usual extension classes (I limited them, they were more universal, 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 } }; } 
+4
source

Source: https://habr.com/ru/post/1411333/


All Articles