0

我正在尝试使用 kendo ui 网格和 MVC 创建批量插入、更新、删除。这是我的视图和控制器代码。

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

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

<script src="~/Scripts/kendo.all.min.js"></script>
<script src="~/Scripts/kendo.custom.js"></script>

<div>    
    <div id="grid"></div>
    <script>
        $(document).ready(function () {
            $("#grid").kendoGrid({

                editable: true,
                pageable: true,
                sortable: true,
                filterable: true,
                toolbar: ["create", "save", "cancel"],

                columns: [
                        "Name",
                        { command: "destroy", title: "Delete", width: "110px" }
                ],

                dataSource: {
                    transport: {
                        create: {
                            url: "@Url.Action("Create", "VendorType")",
                            dataType: "json",
                            type: "POST" 
                        },
                        read: {
                            url: "@Url.Action("Read", "VendorType")", 
                            contentType: "application/json",
                            type: "POST"
                        }
                    },
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true,
                    pageSize: 5,
                    schema: {
                        data: "Data",
                        total: "Total",
                        model: { 
                            id: "Id",
                            fields: {
                                Id: { editable: false, nullable: true },
                                Name: { validation: { required: true } }
                            }
                        }
                    }
                }
            });
        });
    </script>    
</div>

控制器 :

[HttpPost]
public ActionResult Read(int take, int skip, IEnumerable<Sort> sort, Kendo.DynamicLinq.Filter filter)
{

    var result = db.VendorTypes
        .OrderBy(p => p.Id)
        .ToDataSourceResult(take, skip, sort, filter);

    return Json(result);

}

[HttpPost]
public ActionResult Create(IEnumerable<VendorType> types)
{
    var result = new List<VendorType>();
    try
    {
        foreach (var category in types)
        {
            result.Add(category);
            // Add the entity
            db.VendorTypes.Add(category);
        }
        db.SaveChanges();

    }
    catch (DbEntityValidationException e)
    {
        foreach (var eve in e.EntityValidationErrors)
        {
            Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                eve.Entry.Entity.GetType().Name, eve.Entry.State);
            foreach (var ve in eve.ValidationErrors)
            {
                Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                    ve.PropertyName, ve.ErrorMessage);
            }
        }
    }
    return Json(result);
}

现在读取操作工作正常。但是当我尝试保存条目时,CREATE、UPDATE 或 DELETE 方法中的对象列表始终为空。我可以看到在浏览器中传递的值,但在控制器中它们不可用。有人能指出我犯的错误吗?

4

1 回答 1

0

我找到了解决方案。您在控制器中作为参数的模型名称需要使用 kendo UI 的“ParameterMap”进行映射。

 parameterMap: function (data, operation) {
                            if (operation != "read") {
                                var result = {};
                                for (var i = 0; i < data.models.length; i++) {
                                    var site = data.models[i];
                                    for (var member in site) {
                                        result["projects[" + i + "]." + member] = site[member];
                                    }
                                }
                                return result;
                            } else {
                                return JSON.stringify(data)
                            }
                        }

由于我的参数集合被定义为“项目”,因此我的控制器操作方法应该与参数具有相同的名称。

IE

public ActionResult Create(IEnumerable<Project> projects)
{

}

希望这会对某人有所帮助。

于 2015-12-12T06:38:56.250 回答