1

我正在使用 Kendo 网格来显示员工数据并执行创建、更新和删除。读取操作执行得很好,但是剩下的三个操作没有反映回数据库

这是我尝试过的代码

<div id="grdCRUD">
</div>
    <script type="text/javascript">
        $(document).ready(function () {
 dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    url: "GridWebService.asmx/GetData"
                },
                update: {
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: function (EmpNames) {
                        return "GridWebService.asmx/UpdateEmp" + EmpNames.eid
                    },
                    dataType: "json"
                },
                destroy: {
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    url: "GridWebService.asmx/DeleteEmp"
                },
                create: {
                    url: "",
                    dataType: "jsonp"
                },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return { models: kendo.stringify(options.models) };
                    }
                }
            },
            batch: true,
            pageSize: 6,
            schema: {
                data: "d",
                model: {
                    id: "eid",
                    fields: {
                        ename: { validation: { required: true} },
                        age: { type: "number", validation: { required: true, min: 1} },
                        salary: { type: "number", validation: { required: true, min: 1} }
                    }
                }
            }
        });
        $("#grdCRUD").kendoGrid({
            dataSource: dataSource,
            pageable: {
                refresh: true,
                pageSizes: true
            },
            height: 300,
            toolbar: ["create"],
            columns: [
                                    { field: "ename", title: "EmployeeName", width: "150px" },
                                    { field: "age", title: "EmployeeAge", width: "150px" },
                                    { field: "salary", title: "EmployeeSalary", width: "100px" },
                                    { command: ["edit", "destroy"], title: "&nbsp;", width: "210px" }
                                 ],
            editable: "popup"
        });
 });
</script>

这是我的网络服务

[WebMethod]
public List<EmpNames> GetData()
{
    SqlDataAdapter da = new SqlDataAdapter("select * from Emp", con);
    DataSet ds = new DataSet();
    da.Fill(ds, "emp");
    return LstEmpNames(ds);
}

public List<EmpNames> LstEmpNames(DataSet ds)
{
    List<EmpNames> objenamelst = new List<EmpNames>();
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        EmpNames objemp = new EmpNames();

        objemp.eid = Convert.ToInt32(ds.Tables[0].Rows[i][0]);
        objemp.ename = ds.Tables[0].Rows[i][1].ToString();
        objemp.age = Convert.ToInt32(ds.Tables[0].Rows[i][2]);
        objemp.salary = Convert.ToInt32(ds.Tables[0].Rows[i][3]);
        objenamelst.Add(objemp);
    }
    return objenamelst;
}
[WebMethod]
public DataSet DeleteEmp(int id)
{
    con.Open();
    SqlCommand cmd = new SqlCommand("delete  Emp where eid=" + id, con);
    cmd.ExecuteNonQuery();
    con.Close();
    return null;
}

[WebMethod]
public DataSet CreateEmp()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("Insert into Emp values", con);
    cmd.ExecuteNonQuery();
    con.Close();
    return null;
}

[WebMethod]
public DataSet UpdateEmp(int eid)
{
    con.Open();
    SqlCommand cmd = new SqlCommand("update emp set ename='SHANKI',age=25,salary=6000 where eid=1", con);
    con.Close();
    return null;
}  

是我错过了什么,或者如果代码有误,您能否提供任何示例代码非常感谢。

4

1 回答 1

2

这是一个使用 ASP.NET Web 服务的完整工作的 CRUD 应用程序:https ://github.com/telerik/kendo-examples-asp-net/tree/master/grid-web-service-crud

您的实现的问题是方法的签名 - 检查链接的示例方法的外观。

关于从 JavaScript 调用 ASP.NET Web 服务的详细说明可以在这篇优秀的博客文章中找到:http: //encosia.com/using-jquery-to-consume-aspnet-json-web-services/

于 2012-12-27T17:14:35.260 回答