Kendo UI Grid Update button not firing

2019-09-18 10:17发布

问题:

I am developing a KendoUI Grid with Inline editable option in javascript and can't make Update button to fire click event and post the data to server side update event. Clicking on Update button won't even update the grid on client.

Hope someone can help me point out what I am doing wrong here.

This is not a duplicate to this as I have tired the jfiddle link in the answer and it is not working too. kendo UI grid update function wont fire

<div id="grid"></div>

 @section Scripts{

<script type="text/javascript">

    $(function () {
        var dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: "Home/GetPupilsAsJson",
                    dataType: 'json'
                },                    
                update: {
                    url: "Home/UpdatePupils",
                    dataType: 'json',
                    type: 'POST'
                }
            },
            pageSize: 5,
            autoSync: true                
        });

        $('#grid').kendoGrid({
            dataSource: dataSource,
            editable: "inline",
            pageable: true,
            columns: [{
                field: "Id",
                title: "Id",
                width: 150,
                hidden: true
            }, {
                field: "Firstname",
                title: "Firstname",
                width: 150
            }, {
                field: "Lastname",
                title: "Lastname",
                width: 150
            }, {
                field: "DateOfBirth",
                title: "DateOfBirth",
                width: 150
            }, {
                field: "Class",
                title: "Class",
                width: 150
            }, {
                field: "Year",
                title: "Year",
                width: 150
            },
            {
                command: ["edit"],
                width: 150
            }]
        });       
    });
</script>    
 }

HomeController

 public ActionResult GetPupilsAsJson()
    {            
        return Json(GetPupils(), JsonRequestBehavior.AllowGet);
    }

[AcceptVerbs(HttpVerbs.Post)]
    [HttpPost]
    public void UpdatePupils(Pupil p)
    {
          //never reach here
    }

回答1:

I don't know why but fixed it by putting schema information.

schema: {
        model: {
            id: "Id",
            fields: {
                Firstname: { editable: true, validation: { required: true } },
                Lastname: { validation: { required: true } },
                DateOfBirth: { validation: { required: true } },
                Class: { validation: { required: true } },
                Year: { validation: { required: true } }
            }
        }
    }


回答2:

Use @Url.Action("GetPupilsAsJson", "Home")' so no need to pass base url in your update action like this BASEURL+ "Home/GetPupilsAsJson".

 var dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: '@Url.Action("GetPupilsAsJson", "Home")',
                    dataType: 'json'
                },                    
                update: {
                    url:'@Url.Action("UpdatePupils", "Home")',
                    dataType: 'json',
                    type: 'POST'
                }
            },
            pageSize: 5,
            autoSync: true                
        });


回答3:

use parameter map to pass model values

<script type="text/javascript">

    $(function () {
        var dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: '@Url.Action("GetPupilsAsJson", "Home")',,
                    dataType: 'json'
                },                    
                update: {
                    url: '@Url.Action("UpdatePupils", "Home")',
                    dataType: 'json',
                    type: 'POST'
                },
                parameterMap: function (options, operation) {
                   if (operation !== "read" && options.models) {
                      return { models: kendo.stringify(options.models) };
                       }
                }
            },
            pageSize: 5,
            autoSync: true                
        });

Call Controller with parameterMap

public JsonResult UpdatePupils(string models)
        {

            return Json(..);
        }


回答4:

Is any of your cell text has HTML tags like < or >, remove them and click on update. The update event will fire.