1

I'm trying to implement the kendo grid in my mvc 3 apps. I'm interested in batch editing. I need to send batch data from view to controller action method.

Here is my code for view:

<!DOCTYPE html>
<html>
<head>
<title>
    This is Home!!
</title>

<link href="../../Content/kendo/2013.1.319/kendo.common.min.css" rel="stylesheet" />

<link href="../../Content/kendo/2013.1.319/kendo.metro.min.css" rel="stylesheet" />
<script src="../../Scripts/jquery-2.0.2.min.js"></script>
<script src="../../Scripts/kendo/2013.1.319/kendo.web.min.js"></script>

<script type="text/javascript">


    var dataSource = new kendo.data.DataSource({
        schema: {
            model: {
                id: "EmployeeID",
                fields: {
                    EmployeeID: { editable: false, nullable: true },
                    EmployeeName: { validation: { required: true } }

                }
            }
        },
        transport: {
            read: {
                url: "/Home/GetData",
                type: "GET"
            },
            update: {
                url: "/Home/Update",
                type: "POST",
                contentType: 'application/json'
            },
            destroy: {
                url: "/Home/Destroy",
                type: "POST"
            },


            create: {
                url: "/Home/Create",
                type: "POST",
                contentType: 'application/json'
            },


            pageSize: 20,


            parameterMap: function (options, operation) {
                if (operation !== "read" && options.models) {
                    return { models: kendo.stringify(options.models) };
                }
            }
        }

    });





    $(document).ready(function () {


        $("#grid").kendoGrid({
            dataSource: dataSource,
            navigatable: true,
            pageable: true,
            height: 430,
            sortable: true,
            toolbar: ["create", "save", "cancel"],
            columns: [

                { field: "EmployeeID", title: "Employee ID", width: 110 },
                { field: "EmployeeName", title: "Employee Name", width: 110 },

                { command: "destroy", title: "Delete", width: 90 }],
            editable: true,
            selectable: "multiple row",
            groupable: true,
            navigatable: true
        });
    });



</script>

</head>
<body>

<div id="example" class="k-content">
    <div id="grid"></div>

</div>

</body>
</html>

Controller code:

 [HttpPost]
 public JsonResult Update(List<Employee> model) //Parameter gets no data.
 {
     var obj = new Employee();
     //return View();
     return Json(obj);
 }

//Parameter gets no data.
[HttpPost]
public ActionResult Create(List<Employee> model) 
{
   return View("Index");
}

if I'm not wrong, I'm doing something wrong in parameter mapping or the signature of the action method, can't figure out what? Please help. Thanks.

4

3 回答 3

2

你做错的是你没有说它需要是批处理的,所以你实际上是在完成编辑EmployeeName(退出编辑模式)后立即将数据发送到服务器,但是parameterMap在非批处理发送时你的功能不正确模式,因为那时没有model输入options(只是直接数据)。

所以,要么添加batch: trueDataSoure定义中(如果你想进入batch模式):

var dataSource = new kendo.data.DataSource({
    batch: true,
    schema: {
        model: {
            id: "EmployeeID",
            fields: {
                EmployeeID: { editable: false, nullable: true },
                EmployeeName: { validation: { required: true } }

            }
        }
    },
    ...

或更改parameter地图以返回options(仅当您不想使用时batch):

parameterMap: function (options, operation) {
    if (operation !== "read") {
        return options;
    }
}
于 2013-07-07T09:28:08.500 回答
0

我遇到了同样的问题,并尝试了我在网上找到的所有选项,唯一对我有用的是这个

public ActionResult Products_Update([DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<ProductViewModel> products)
{
    // Will keep the updated entitites here. Used to return the result later.
    var entities = new List<Product>();
    if (ModelState.IsValid)
    {
        using (var northwind = new NorthwindEntities())
        {
            foreach (var product in products)
            {
                // Create a new Product entity and set its properties from the posted ProductViewModel.
                var entity = new Product
                {
                    ProductID = product.ProductID,
                    ProductName = product.ProductName,
                    UnitsInStock = product.UnitsInStock
                };
                // Store the entity for later use.
                entities.Add(entity);
                // Attach the entity.
                northwind.Products.Attach(entity);
                // Change its state to Modified so Entity Framework can update the existing product instead of creating a new one.
                northwind.Entry(entity).State = EntityState.Modified;
                // Or use ObjectStateManager if using a previous version of Entity Framework.
                // northwind.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
            }
            // Update the entities in the database.
            northwind.SaveChanges();
        }
    }
    // Return the updated entities. Also return any validation errors.
    return Json(entities.ToDataSourceResult(request, ModelState, product => new ProductViewModel
    {
        ProductID = product.ProductID,
        ProductName = product.ProductName,
        UnitsInStock = product.UnitsInStock
    }));
}

这个例子我从 Telerik 网站http://docs.telerik.com/aspnet-mvc/helpers/grid/editing/batch-editing

于 2016-12-06T18:22:55.630 回答
0

尝试:

在传输“更新”内容类型标头中设置为:

'contentType: "application/json; charset=utf-8",'

并在 parameterMap 选项中使用:

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

或者

return JSON.stringify({ models: options.models });
于 2017-10-05T10:36:32.930 回答