Telerik MVC 网格更新动态模型

问题描述 投票:0回答:1

所以我有由 IEnumerable 模型填充的 Telerik MVC 网格,因为我将在网格上添加动态产品列。用户将能够对每个产品的每行进行编辑,这将是单元内批量编辑。

现在,我遇到了如何在保存更改期间获取脏/更新记录的问题。我似乎无法检索调用 gridUpdate 时的更新记录。

@(Html.Kendo().Grid<dynamic>()
.Name("myGrid")
.ToolBar(tootlbar => tootlbar.Save())
.Columns(column =>
{
    column.Bound("Id").Hidden(true);
    column.Bound("LastName");
    column.Bound("FirstName");

    //--Products will be dynamic from Database Table
    column.Bound("ProductA");
    column.Bound("ProductB");
    column.Bound("ProductC");

})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.AutoBind(true)
.DataSource(dataSource => dataSource
    .Ajax()
    .Batch(true)
    .Model(model =>
    {
        model.Id("Id");

        model.Field("Id", typeof(int)).Editable(false);
        model.Field("LastName", typeof(int)).Editable(false);
        model.Field("FirstName", typeof(string)).Editable(false);

        model.Field("ProductA", typeof(string)).Editable(true);
        model.Field("ProductB", typeof(string)).Editable(true);
        model.Field("ProductC", typeof(string)).Editable(true);
    })
    .Read(read => read.Action("gridRead", "myController"))
    .Update(update => update.Action("gridUpdate", "myController"))
))



public JsonResult gridRead([DataSourceRequest] DataSourceRequest request)
    {
        try
        {
            List<dynamic> list = new List<dynamic>();
            
            for(int i = 0; i <= 50; i++)
            {
                dynamic obj = new ExpandoObject();
                obj.Id = i;
                obj.LastName = "Last Name " + i.ToString();
                obj.FirstName = "First Name " + i.ToString();

                //--Products will be dynamic from Database Table
                obj.ProductA = "Product A" + i.ToString();
                obj.ProductB = "Product A" + i.ToString();
                obj.ProductC = "Product A" + i.ToString();

                list.Add(obj);
            }

            return Json(list.ToDataSourceResult(request));
        }
        catch (Exception ex)
        {
            return Json(new DataSourceResult() { Errors = new { Message = "Error: " + ex.Message, ExceptionMessage = ex.Message } });
        }
    }

    public JsonResult gridUpdate([DataSourceRequest] DataSourceRequest request, IEnumerable<dynamic> data)
    {

        foreach(dynamic obj in data)
        {
            //--Always Null --//
            string Id1 = obj.Id; 

            //-- Also always null --//
            string Id2 = GetProperty(obj, "Id"); 
        }

        //--- GET BY ID AND UPDATE RECORD (CODE HERE) --//

        return Json(data.ToDataSourceResult(request));
    }

    private object GetProperty(dynamic obj, string propertyName)
    {
        var objDict = obj as IDictionary<string, object>;
        if (objDict.ContainsKey(propertyName))
        {
            var value = objDict[propertyName];

            if (value != null)
                return value;
        }

        return null;
    }
kendo-ui grid telerik kendo-grid telerik-mvc
1个回答
0
投票

通过批量编辑,网格将提交带有前缀

models
的更新值,因此您可以尝试以下操作:

 public IActionResult gridUpdate([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")] IFormCollection models)
 {
  ...
 }
© www.soinside.com 2019 - 2024. All rights reserved.