This is a migrated thread and some comments may be shown as answers.

MVC Grid bound to dynamic data only sends empty object to controller

1 Answer 652 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Casey
Top achievements
Rank 1
Casey asked on 07 Jan 2019, 05:06 PM

I've created some grids that are bound to dynamic data.  I used dapper to get the data i want from my database.  for each grid i have set the model id and fields with the correct data types.  my problem is, when creating/updating/deleting a record, the grid only sends an empty object to the appropriate controller.  using reflection i can see this object has no fields, properties, or methods.  Below I've included my grid configuration (Razor) and the update method.  Can anyone see where i've gone wrong?  i followed the example at this link to get as far as i currently am https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/grid/grid-bind-to-collection-dynamic/KendoMVCWrappers 

 

Grid

 

<div class="container">

    @{ 
        int ctr = 0;
        foreach(var table in Model)
        {
        <div style="margin-top:50px;">
            @(Html.Kendo().Grid(table)
                 .Name($"Grid_{ctr.ToString()}")
                 .DataSource(ds=>ds
                 .Ajax()
                 .Model(m=> {
                     var dict = (IDictionary<string, object>)table.First();
                     var keyfield = dict.ElementAt(0).Key;
                     m.Id(keyfield);
                     foreach (var i in dict  )
                     {
                         string key = i.Key;
                         var valuetype = i.Value.GetType();
                         m.Field(key,valuetype);
                     }
                 })
                 .Events(e=> { e.Sync("syncHandler");e.Change("syncHandler"); })
                 .Update(update=>update.Action("Update","MultiTable"))
                 .Create(create=>create.Action("Create","MultiTable"))
                 )
                 .BindTo(table)
                 .Pageable()
                 .Events(e => { e.Save("saveHandler");e.SaveChanges("saveChangesHandler"); })
                 .ToolBar(toolBar => { toolBar.Create();toolBar.Save(); })
                 .Editable(e=>e.Mode(GridEditMode.InCell)))
        </div>
            ctr++;
        }
    }
</div>

 

update method (dynamic id is always blank/empty)

 

[HttpPost]
        public async Task<IActionResult> Update([DataSourceRequest]DataSourceRequest request, dynamic id)
        {
            //var m = id.model;
            Type myType = id.GetType();
            FieldInfo[] fields = myType.GetFields();
            foreach (var field in fields)
            {
                var name = field.Name;
                var tmp = field.GetValue(null);

                if (tmp is int)
                {
                    Console.Write("int");
                }
                else if (tmp is string)
                {
                    Console.Write("string");
                }
            }

            IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

            foreach (PropertyInfo prop in props)
            {
                object propValue = prop.GetValue(id, null);

            }
            return View();

        }

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Alex Hajigeorgieva
Telerik team
answered on 09 Jan 2019, 01:45 PM
Hello, Casey,

Thank you for the provided code snippet and the resource that you are using.

Since in the code, it becomes apparent that the Kendo UI Grid for ASP.NET MVC uses in-cell editing, then you should apply all the recommendations to it that are applicable to a non-dynamic grid:

https://demos.telerik.com/aspnet-mvc/grid/editing
https://docs.telerik.com/aspnet-mvc/helpers/grid/editing/batch-editing

- set ServerOperations() to false
- accept a collection of the edited models in the controller
- return JSON, not the View() - in the case of async operations, you may take advantage of the ToDataSourceResultAsync() method

Let me know in case you need further assistance.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Casey
Top achievements
Rank 1
Answers by
Alex Hajigeorgieva
Telerik team
Share this question
or