Dynamically add columns from partial view

Hi!

I am wondering if something like this is possible:


I have an Html.EJ().Grid<object>
...

I want to dynamically map columns from partial view

Something like (?)

  .Columns(col =>
            {
                col.Field("Id").HeaderText("Id").IsPrimaryKey(true).Visible(false).Add();
                col.Field("Order").HeaderText(FormsResources.Order).Priority(1).Add();
                    ************ HERE I WANT TO INJECT ADDITIONAL COLUMNS **********
                col.Field("Label").HeaderText(FormsResources.Label).Priority(2).Add();
             }


I thought about something like

Html.RenderPartial("_PartialWhatever", new ViewDataDictionary(new
                {
                    ResourceTypeId = Model.Id,
                    Columns = col
                }));

but it doesn't work.

TL;DR; I want to access the col object from the parent view in my _PartialWhatever view


Thanks ;)


1 Reply 1 reply marked as answer

PK Padmavathy Kamalanathan Syncfusion Team October 19, 2020 02:37 PM UTC

Hi Catalin, 
 
Thanks for contacting Syncfusion Forums. 
 
Query: Dynamically add columns to the grid 
 
You can assign columns to the Grid from the server end by passing variable containing the required columns to the view page as shown in the below code, 
 
 
[in server end] 
using Syncfusion.JavaScript.Models; 
 
------------ 
            List<Column> colList = new List<Column>(); 
            colList.Add(new Column() { Field = "OrderID", HeaderText = "Order ID", Width = 75 }); 
            colList.Add(new Column() { Field = "CustomerID", HeaderText = "Customer ID", Width = 80 }); 
            colList.Add(new Column() { Field = "EmployeeID", HeaderText = "EmployeeID", Width = 100 }); 
            ViewBag.Columns = colList; 
 
[in view page] 
 
@using Syncfusion.JavaScript.Models 
@model Syncfusion.JavaScript.Models.GridProperties 
 
    @(Html.EJ().Grid<object>("Grid") 
                  .Datasource(((IEnumerable<object>)ViewBag.dataSource))                                             
                  .Columns(ViewBag.Columns) 
    ) 

 
If the above solution does  not meet your requirement, kindly get back to us with the below details: 
  1. Complete Grid rendering code(both client and server end)
  2. Explain your requirement in detail with exact scenario
  3. Video demonstrating the issue/ requirement
  4. Screenshot of error with stack trace (if any)
 
Regards, 
Padmavathy Kamalanathan 


Marked as answer
Loader.
Up arrow icon