Kendo ui 剑道网格在创建/更新/删除时获取空对象

Kendo ui 剑道网格在创建/更新/删除时获取空对象,kendo-ui,kendo-grid,kendo-asp.net-mvc,jsonresult,Kendo Ui,Kendo Grid,Kendo Asp.net Mvc,Jsonresult,我尝试了一个简单的Kendo UI网格,其中包含一个Employee类的CRUD操作。但是,当我创建/更新/删除时,控制器中的employee对象不会在所有操作中获得相应的值和Id设置为0 <div id="grid"></div> <script> $(document).ready(function () { var crudServiceBaseUrl = "Components/", dataSource

我尝试了一个简单的Kendo UI网格,其中包含一个Employee类的CRUD操作。但是,当我创建/更新/删除时,控制器中的employee对象不会在所有操作中获得相应的值和Id设置为0

<div id="grid"></div>
<script>
    $(document).ready(function () {
        var crudServiceBaseUrl = "Components/",
            dataSource = new kendo.data.DataSource({
                type: "odata",
                transport: {
                    read: {
                        url: crudServiceBaseUrl + "GetEmployees",
                        dataType: "json"
                    },
                    update: {
                        url: crudServiceBaseUrl + "UpdateEmployee",
                        dataType: "json",
                        type: "Post"
                    },
                    destroy: {
                        url: crudServiceBaseUrl + "DeleteEmployee",
                        dataType: "json",
                        type: "Post"
                    },
                    create: {
                        url: crudServiceBaseUrl + "CreateEmployee",
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        type: "POST",
                    },
                    parameterMap: function (data, type) {
                        return kendo.stringify(data);
                    },
                },
                batch: true,
                pageSize: 20,
                type: "json",
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors",
                    model: {
                        id: "Id",
                        fields: {
                            Id: { editable: false, nullable: true },
                            FullName: { validation: { required: true } },
                            Designation: { validation: { required: true } },
                        }
                    }
                }
            });

        $("#grid").kendoGrid({
            dataSource: dataSource,
            pageable: true,
            height: 430,
            toolbar: ["create"],
            columns: [
                { field: "Id" },
                { field: "FullName" },
                { field: "Designation" },
                { command: ["edit", "destroy"], title: "&nbsp;", width: "160px" }],
            editable: "popup"
        });
    });
</script>
每次按Create/Update/Detele时,控制器操作中的员工值如下:

Id = 0;
FullName = null;
Designation = null;
请给出解决方案

当我使用tbe网格时,代码如下

@(Html.Kendo().Grid<Employee>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.Id);
        columns.Bound(p => p.FullName);
        columns.Bound(p => p.Designation);
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(160);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .Pageable()
    .Sortable()
    .Scrollable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Events(events => events.Error("error_handler"))
        .Model(model => model.Id(p => p.Id))
        .Create(update => update.Action("CreateEmployee", "Components"))
        .Read(read => read.Action("GetEmployees", "Components"))
        .Update(update => update.Action("UpdateEmployee", "Components"))
        .Destroy(update => update.Action("DeleteEmployee", "Components"))
    )
)
@(Html.Kendo().Grid())
.名称(“网格”)
.列(列=>
{
columns.Bound(p=>p.Id);
columns.Bound(p=>p.FullName);
列绑定(p=>p.Designation);
Command(Command=>{Command.Edit();Command.Destroy();}).Width(160);
})
.ToolBar(ToolBar=>ToolBar.Create())
.Editable(可编辑=>Editable.Mode(GridEditMode.PopUp))
.Pageable()
.Sortable()
.Scrollable()
.HtmlAttributes(新的{style=“height:430px;”})
.DataSource(DataSource=>DataSource
.Ajax()
.页面大小(20)
.Events(Events=>Events.Error(“错误处理程序”))
.Model(Model=>Model.Id(p=>p.Id))
.Create(update=>update.Action(“CreateEmployee”、“Components”))
.Read(Read=>Read.Action(“GetEmployees”、“Components”))
.Update(Update=>Update.Action(“UpdateEmployee”,“Components”))
.Destroy(update=>update.Action(“删除员工”、“组件”))
)
)

它与相同的控制器操作完美配合。

尝试设置为
“application/json”
。这可能只是因为它向服务器发送了错误的内容类型,因此服务器不知道如何解析它。

通过从数据源中删除批处理:true来解决此问题。

如果您在浏览器开发工具的“网络”选项卡中进行检查,您是否可以看到哪些数据被发布到服务器?发布数据非常完美。当我删除时,它传递了{“models”:[{“Id”:7,“FullName”:“ABC”,“Designation”:“XYZ”}]}:还有事件,即DeleteEmployee,方法:Post,状态:500 Type:text/html,这里的Type应该是:application/jsonRESOLVED:为了让它工作,我删除了以下代码。。parameterMap:function(data,type){return kendo.stringify(data);},batch:true,并添加了。。。“type”:“aspnetmvcajax”,需要单独回答!
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult CreateEmployee([DataSourceRequest] DataSourceRequest request, Employee employee)
    {
        if (employee != null && ModelState.IsValid)
        {
            using (EmployeeDBDataContext context = new EmployeeDBDataContext())
            {
                EmployeeTable newEmployee = new EmployeeTable();

                newEmployee.FullName = employee.FullName;
                newEmployee.Designation = employee.Designation;

                context.EmployeeTables.InsertOnSubmit(newEmployee);
                context.SubmitChanges();
            }
        }

        return Json(new[] { employee }.ToDataSourceResult(request, ModelState));
    }

    public ActionResult GetEmployees([DataSourceRequest] DataSourceRequest request)
    {
        var lstConfiguredEmails = new List<Employee>();

        using (EmployeeDBDataContext context = new EmployeeDBDataContext())
        {
            lstConfiguredEmails = (from e in context.EmployeeTables
                                   select new Employee
                                              {
                                                  Id = e.Id,
                                                  FullName = e.FullName,
                                                  Designation = e.Designation
                                              }).ToList();
        }

        return Json(lstConfiguredEmails.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult DeleteEmployee([DataSourceRequest] DataSourceRequest request, Employee employee)
    {
        if (employee != null)
        {
            using (EmployeeDBDataContext context = new EmployeeDBDataContext())
            {
                EmployeeTable deleteEmployee = (from e in context.EmployeeTables
                                                where e.Id == employee.Id
                                                select e).SingleOrDefault();

                context.EmployeeTables.DeleteOnSubmit(deleteEmployee);
                context.SubmitChanges();
            }
        }

        return Json(ModelState.ToDataSourceResult());
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult UpdateEmployee([DataSourceRequest] DataSourceRequest request, Employee employee)
    {
        if (employee != null && ModelState.IsValid)
        {
            using (EmployeeDBDataContext context = new EmployeeDBDataContext())
            {
                EmployeeTable updateEmployee = (from e in context.EmployeeTables
                                                where e.Id == employee.Id
                                                select e).SingleOrDefault();

                updateEmployee.Id = employee.Id;
                updateEmployee.FullName = employee.FullName;
                updateEmployee.Designation = employee.Designation;

                context.SubmitChanges();
            }
        }

        return Json(ModelState.ToDataSourceResult());
    }
@(Html.Kendo().Grid<Employee>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.Id);
        columns.Bound(p => p.FullName);
        columns.Bound(p => p.Designation);
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(160);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .Pageable()
    .Sortable()
    .Scrollable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Events(events => events.Error("error_handler"))
        .Model(model => model.Id(p => p.Id))
        .Create(update => update.Action("CreateEmployee", "Components"))
        .Read(read => read.Action("GetEmployees", "Components"))
        .Update(update => update.Action("UpdateEmployee", "Components"))
        .Destroy(update => update.Action("DeleteEmployee", "Components"))
    )
)