如何从IList为Kendo Grid进行创建更新和删除数据?

时间:2015-02-13 07:40:44

标签: javascript asp.net-mvc kendo-ui asp.net-ajax kendo-grid

这是我的kendogrid代码:

KendoDemosFactory.controller("MyCtrl", function ($scope, $sessionStorage) {
        $scope.mainGridOptions = {
            dataSource: {                
                transport: {
                    read: {url:"/User/GetUserDetail",datatype:"json"},
                    update: { url: "/User/UpdateUser", type: "POST" },
                    destroy:{ url:"/User/DeleteUser",type:"POST"},
                    Create:{url:"/User/CreateUser",type:"POST"}
                },
                parameterMap: function(options, operation) {
                    if (operation !== "read" && options.models) {
                        return {models: kendo.stringify(options.models)};
                    }
                },
                
                batch: true,
                pageSize: 5,
                serverPaging: false,
                serverSorting: false
            },
            sortable: true,
            pageable: true,
            navigatable: true,
            toolbar: ["create", "save", "cancel"],
            dataBound: function () {
                this.expandRow(this.tbody.find("tr.k-master-row").first());
            },
            columns: [{
                field: "FirstName",
                title: "First Name",
                width: "80px"
            }, {
                field: "LastName",
                title: "Last Name",
                width: "80px"
            }, {
                field: "Address",
                width: "80px"
            }, {
                field: "Email",
                width: "100px"
            }, {
                field: "Gender",
                width: "80px"
            },
            {
                field: "Salary",
                width:"100px"
            },
            { command: "destroy", title: " ", width: 100 }
            ],
            editable:true
        };
    })

这是我的控制器,包含我的用户列表和所有方法:

  public ActionResult GetUserDetail()
        {
            IList<UserModel> userList = new List<UserModel>()
            {
            new UserModel{
                id=1,
                FirstName = "Pawan",
                LastName = "Kapoor",
                Address = "Mohali",
                Email = "test@test.com",
                Gender = "Male",
                Salary = 25000
            },           

          new UserModel
            {
                id=2,
                FirstName = "Ayan",
                LastName = "Banerjee",
                Address = "Bangalore",
                Email = "test1@test.com",
                Gender = "Male",
                Salary = 30000
            },
}
}


[HttpPost]
        public ActionResult CreateUser()
        {            
            IList<UserModel> userList = new List<UserModel>();
            return Json(userList, JsonRequestBehavior.AllowGet);                        
        }

        [HttpPost]
        public ActionResult UpdateUser()
        {
            IList<UserModel> userList = new List<UserModel>();
            return Json(userList, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult DeleteUser()    
        {
            IList<UserModel> userList = new List<UserModel>();
            return Json(userList, JsonRequestBehavior.AllowGet);
        }       

我的kendo Grid使用可编辑的功能和所有CRUD控件正常填充,但我无法使用CRUD方法将数据与控制器绑定(它们没有受到攻击,并且任何crud功能都没有被持久化)。请帮忙

1 个答案:

答案 0 :(得分:0)

让我给你一些笔记来纠正你的代码。

  • 如果您不想进行批量更新,请仅向控制器发送当前行。

    parameterMap: function (options, operation) {
        if (operation !== "read" && options.models) {
             return { models: options.models[0] };
        }
    }
    
  • 更正您的更新操作方法如下:MVC将从网格发送的数据绑定到UserModel。

    [HttpPost]
    public ActionResult UpdateUser(UserModel model)
    {
        // do something with model 
        return Json(model, JsonRequestBehavior.AllowGet);
    }
    

This link will lead you to solve your problem