Grid MVC dynamic column data?

4 posts, 0 answers
  1. Peter
    Peter avatar
    8 posts
    Member since:
    Mar 2016

    Posted 25 Apr Link to this post

    Hi!

    I use Kendo UI for MVC 2016.1.301, with MVC 6 (Core 1.0). I have a grid with some fixed (bound) columns, and a few dynamic columns. I have managed to add the columns dynamically, with titles. The data type for these dynamic columns are bool. I've found various samples but none of them works, either they don't compile or they have no effect. I must add I'm new to the Razor engine so I don't understand exactly what's happening at all times.

    So, how do I replace the [X] with data from the view model? I've tried variations on this theme, using .Bound and .Template, but nothing compiles either compile-time or run-time:

    1.for (int i = 0; i < Model.PresetsList.PresetsList.Count(); i++)
    2.{
    3.    string titel = Model.PresetsList.PresetsList[i].PresetsName;
    4.    int columnIndex = i;
    5.    columns.Bound(cgi => cgi.MadeChoicePresetsList[columnIndex].IsSelected.ToString()).Title(titel);
    6.}

     

    Here's the current code which displays a [X] instead of data:

    01.    @(Html.Kendo().Grid<C.ViewModels.ChoiceGroupItemViewModel>()
    02.        .Name("choicegroupitemsgrid")
    03.        .Columns(columns =>
    04.        {
    05.            columns.Bound(col => col.ChoiceGroupID).Title("gruppID");
    06.            columns.Bound(col => col.ItemID).Title("artikelID");
    07.            columns.Bound(col => col.ManufacturerItemID).Title("Tillv. artikelnr");
    08.            columns.Bound(col => col.ItemName).Title("Benämning");
    09.            columns.Bound(col => col.CustomerPayablePriceInclTax).Title("Pris").HtmlAttributes(new { align = "right" });
    10. 
    11.            foreach (var presets in Model.PresetsList.PresetsList)
    12.            {
    13.                columns.Template("[X]").Title(presets.PresetsName);
    14.            }
    15.        }
    16.        )
    17.        .Scrollable()
    18.        .Pageable(pgbl => pgbl
    19.            .Refresh(true)
    20.            .PageSizes(true)
    21.            .ButtonCount(5)
    22.        )
    23.        .Sortable(sortable => { sortable.SortMode(GridSortMode.SingleColumn); })
    24.        .DataSource(datasrc => datasrc
    25.            .Ajax()
    26.            .Read(read => read
    27.                .Action("ChoiceGroupItems", "ChoiceGroup", new { area = "Backoffice", id = Model.ChoiceGroupID })
    28.            )
    29.            .PageSize(20)
    30.        )
    31. 
    32.)

  2. Peter
    Peter avatar
    8 posts
    Member since:
    Mar 2016

    Posted 26 Apr in reply to Peter Link to this post

    Hi! Found the answer, I was almost there!

    All I had to do was to bind to the bool property, not to a .ToString() function:

    1.for (int i = 0; i < Model.PresetsList.PresetsList.Count(); i++)
    2.{
    3.    string titel = Model.PresetsList.PresetsList[i].PresetsName;
    4.    int columnIndex = i;
    5.    columns.Bound(cgi => cgi.MadeChoicePresetsList[columnIndex].IsSelected).Title(titel);
    6.}

    The values rendered by the grid are rendered as "true" or "false".

    Peter

  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Peter
    Peter avatar
    8 posts
    Member since:
    Mar 2016

    Posted 12 May in reply to Peter Link to this post

    I thought I was done, but not obviously not quite. When I edit the line (GridEditMode.InLine) and click Update, the view model that is posted to my controller does not contain any of the dynamic column's data. The posted data contains the other fields (i.e. TenantPayablePriceInclTax).

    The model for the dynamic data is (other properties omitted for brevity):

    public string TenantPayablePriceInclTax { get; set; }
    public List<MadeChoicePresets> MadeChoicePresetsList;

    Any hints are appreciated.

  5. Kostadin
    Admin
    Kostadin avatar
    1711 posts

    Posted 16 May Link to this post

    Hello Peter,

    I would recommend you to check out the following example which demonstrates how to update a dynamically added columns.

    Regards,
    Kostadin
    Telerik
    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
Back to Top
UI for ASP.NET MVC is VS 2017 Ready