Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 78072

ToDataSourceResult with Dictionary

$
0
0
Hello,

Note that the ToDataSourceResult require a class with defined fields in order to execute sorting and filtering functionality. The MVC framework is working with models and the required functionality can not be achieved for  a list of dictionaries.
Possible appraoch is to map the dictionary collection with Select before the ToDataSourceResult extension is applied, which is almost the same as creating a Model:
public ActionResult EditingInline_Read([DataSourceRequest] DataSourceRequest request)
{
    List<Dictionary<string, dynamic>> test = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, dynamic>>();
    IEnumerable<ProductViewModel> products = productService.Read();
    foreach (var item in products)
    {
        Dictionary<string, dynamic> dict = new Dictionary<string, dynamic>();
        dict.Add("ProductID", item.ProductID);
        dict.Add("ProductName", item.ProductName);
        dict.Add("UnitPrice", item.UnitPrice);
        test.Add(dict);
    }
    var t = test.Select(item=> new ProductViewModel(){
        ProductName = item["ProductName"],
        ProductID = item["ProductID"],
        UnitPrice = item["UnitPrice"],
    }).ToDataSourceResult(request);
    return Json(t);           
}

I hope this helps.

Regards,
Maria Ilieva
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items

Viewing all articles
Browse latest Browse all 78072

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>