Question on Grid Datasource Read with Array Parameters

2 posts, 0 answers
  1. Kolie
    Kolie avatar
    4 posts
    Member since:
    Oct 2012

    Posted 09 Jul 2013 Link to this post

    Hi I am looking into building products using KendoUI MVC as potential tool in future. I have been messing around in the trial and for the most part it is fairly straight forward. I have run into one issue as follows.

    I have a controller for a Contact. It has one view in particular, ViewDetails

    1.public  ActionResult ViewDetails(long[] contactId)
    2.{
    3.    ViewData["contactId"] = contactId;
    4.    return View();
    5.}
    It takes a list of contacts passed in as an array, assigns it to the viewdata and returns the View. This view has a KendoUI Grid in it and the code is as follows

    01.@{
    02.    ViewBag.Title = "ViewDetails";
    03.    Layout = "~/Views/Shared/_Layout.cshtml";
    04.}
    05. 
    06.<h2>ViewDetails</h2>
    07. 
    08. 
    09.@(
    10. 
    11.        @Html.Kendo().Grid<DetailGridViewModel>()
    12.             .Name("Grid")
    13.             .Columns(columns =>
    14.                          {
    15.                              columns.Bound(p => p.ContactId).Hidden();
    16.                              columns.Bound(p => p.ContactNumber);
    17.                              columns.Bound(p => p.ContactName);
    18.                              columns.Bound(p => p.Date).Width(130).Format("{0:d}");
    19.                              columns.Bound(p => p.ContactStatus);
    20.                              columns.Bound(p => p.Type);                            
    21.                              columns.Command(command =>
    22.                                                  {
    23. 
    24.                                                  }).Width(200);
    25.                                                   
    26.                          })
    27. 
    28.     
    29.        .Sortable( )
    30.        .Resizable(resize => resize.Columns(true))
    31.        .Filterable()
    32.        .DataSource(dataBinding =>
    33.                         dataBinding.Ajax().Aggregates(agg => { agg.Add(com => com.Value).Sum();
    34.                                                                agg.Add(p => p.ContactCost).Sum();
    35.                         }).Read(read => read.Action("_SelectDetails", "Contacts").Data(obj => new {contactId = ViewData["contactId"]}))))
    36.    
    The problem I am having occurs on line 35. When I try to assign the Data for "contactId", in the resulting javascript it appears that the only data is System.Int64[] like it called ToString on the data instead of taking the array and passing its contents as multiple named parameters.

    The select function of course fails as there is not a proper contactId parameter passed, it is as follows for reference
    01.public ActionResult _SelectDetails([DataSourceRequest] DataSourceRequest request,long[] contactId)
    02.{
    03.    List<DetailGridViewModel> finalList = new List<DetailGridViewModel>();
    04.    foreach (var conId in contactId)
    05.    {
    06.        var con = _contactService.GetContactById(conId);
    07.        IEnumerable<DetailGridViewModel> coms = con.Select(x => new DetailGridViewModel(x));   
    08.        finalList.AddRange(coms);
    09.    }
    10.     
    11.    return Json(finalList.ToDataSourceResult(request));
    12.}


    Thanks for your assistance. This is one of the few snags I have seen reviewing the product, I hope to get it resolved so I can continue my evaluation in a timely fashion!
  2. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 11 Jul 2013 Link to this post

    Hi Kolie,

     
    Basically passing array from controller to JavaScript variable on the View is not directly related to the KendoUI but is a general programming task. You can use the following approach to stringify correctly the array:

    .Read(read => read.Action("_SelectDetails", "Contacts").Data(obj => Html.Raw(Json.Encode(ViewData["contactId"]))))

     Kind Regards,
    Vladimir Iliev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top