CRUD操作不适用于数据库kendo ui web

时间:2012-12-27 12:55:27

标签: jquery kendo-ui

我正在使用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;
}  

是我错过的任何内容,或者如果代码错误,您可以向我提供任何示例代码,我们非常感激。

1 个答案:

答案 0 :(得分: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/