Why are my KendoGrid “update” parameters always null in the controller?

坚强是说给别人听的谎言 提交于 2019-12-11 13:01:50

问题


I have the following code in my Index.cshtml file:

var dataSource = new kendo.data.DataSource({
    type: "json",
    transport: {
        read: {
            url: '@Url.Action("ReadTeachers", "EducationPortal")',
            dataType: "json"
        },
        update: {
            url: '@Url.Action("UpdateTeachers", "EducationPortal")',
            type: "POST"
        },
        parameterMap: function (data, operation) {
            if (operation != "read"){ 
                var result = {};

                for (var i = 0; i < data.models.length; i++) {
                    var teacher = data.models[i];
                    for (var member in teacher) {
                        result["teacher[" + i + "]." + member] = teacher[member];
                    }
                }

                return result;
            } else {
                return JSON.stringify(data);
            }
        }
    },
    batch: true,
    schema: {
        model: {
            id: "TeacherId",
            fields: {
                TeacherId: { type: "number" },
                FullName: { type: "string" },
                IsHeadmaster: { type: "boolean" }
            }
        }
    }
});

$("#teachers").kendoGrid({
    dataSource: dataSource,
    toolbar: ["create", "save"],
    columns: [
        { field: "FullName", title: "Teacher" },
        { field: "IsHeadmaster", title: "Is a Headmaster?", width: "120px" },
        { command: ["destroy"], title: "&nbsp;", width: "85px" }],
    editable: true
});

This is a standard KendoGrid with "batch" editing. The editing and the grid itself both work fine, but the backend doesn't. When the "update" request goes through, it goes to this controller method:

public void UpdateTeachers(string models)
{
    // this method will have real code later
    Console.Write(models);
}

When I put a breakpoint in here, Visual Studio shows models to be null. Like so:

Why is it null?


回答1:


In your parameterMap: function ()

instead of using

return JSON.stringify(data);

use

return { models: kendo.stringify(data) };

here your data is serialize and attached to a name models now this name models is used as parameter name in your action




回答2:


You need to name the parameter in your controller inside of your parameter map function. Also it looks like you are trying to send the entire data object instead of the model. I would try this within your parameter map function:

parameterMap: function (data, operation) {
        if (operation === "update" && data.models){ 
            return {models: kendo.stringify(data.models) };
        }
    }



回答3:


  1. Use .Batch(true) method inside .DataSource() in view of concerned action method
  2. Use below code inside kendo grid to create toolbar

        .ToolBar(toolbar =>
        {
            toolbar.Create();
            toolbar.Save();
        })
    
  3. Edit mode of grid must be incell to add add more than one entry or in batch mode like below sample code

       .Editable(ed => ed.Mode(GridEditMode.InCell).TemplateName("EditDocket").Window(w => w.Title("Docket").Name("editWindow").Width(377).Height(200).Scrollable(true)))
    
  4. Use below sample code to fetch model details binds to view in controller's action method

    [HttpPost]
    public ActionResult AppEditDocketCreate([DataSourceRequest] DataSourceRequest  request, [Bind(Prefix = "models")]IEnumerable<UDocket> uDockets)
    {
        try
        {
            if (uDockets != null && ModelState.IsValid)
            {
                using (IVSourceEntities db = new IVSourceEntities())
                {
    
                    TBL_Docket tblDocket = new TBL_Docket();
                    foreach (var uDocket in uDockets)
                    {
    
                        tblDocket.DocketName = uDocket.DocketName;
    
                        tblDocket.CreatedDate = System.DateTime.Now;
    
                        db.TBL_Docket.Add(tblDocket);
                        db.SaveChanges(); 
                    }
    
                }
            }
        }
    
    
       catch (DbEntityValidationException e)
        {
    
        }
    
        return Json(ModelState.ToDataSourceResult());
    
    }
    
  5. Only thing that needed is [Bind(Prefix = "models")]IEnumerable uDockets) as parameter to concerned action method



来源:https://stackoverflow.com/questions/22622061/why-are-my-kendogrid-update-parameters-always-null-in-the-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!