删除时Jtable错误

时间:2016-08-24 04:53:44

标签: javascript c# asp.net webforms

这是我第一次尝试Jtable。一切正常,除了删除选项。它给出了一个错误:

  

“与服务器通信时出错”

<script>
$(document).ready(function () {
            $("#department_v").jtable({
                title: 'Department Entry',
                selecting: true, //Enable selecting
                multiselect: true, //Allow multiple selecting
                selectingCheckboxes: true, //Show checkboxes on first column

                paging: true, //Enables paging
                pageSize: 10, //Actually this is not needed since default value is 10.
                sorting: true, //Enables sorting
                defaultSorting: 'id ASC', //Optional. Default sorting on first load.
                actions:
                {

                    listAction: '/frmAdmin.aspx/DepartmentList',
                    deleteAction: '/frmAdmin.aspx/DeleteDepartment',
                    createAction: '/frmAdmin.aspx/CreateNewDepartment',
                    updateAction: '/frmAdmin.aspx/UpdateDepartment',


                },
                fields:
                {
                    id:
                    {
                        title: 'id',
                        key: true,
                        list: true,
                        edit: false

                    },
                    Dept_Name:
                    {
                        title: 'DepartmentName',
                        width: '13%'
                    },



                },



                formCreated: function (event, data) {
                    data.form.find('input[name="Dept_Name"]').addClass('validate[required]');

                    data.form.validationEngine();
                },

                formSubmitting: function (event, data) {
                    return data.form.validationEngine('validate');
                },

                formClosed: function (event, data) {
                    data.form.validationEngine('hide');
                    data.form.validationEngine('detach');
                }




            });

            $('#department_v').jtable('load');
        });
    </script>

[WebMethod(EnableSession = true)]
    public static object DepartmentList(int jtStartIndex, int jtPageSize, string jtSorting)
    {
        try
        {
            //Get data from database
            using (var db = new HRISDBEntities())
            {
                var DepartmentResult = (from DepartmentList in db.tbl_Department select DepartmentList).ToList();
                if (DepartmentResult.Count != 0)
                    return new { Result = "OK", Records = DepartmentResult };
                else return 0;
            }
        }
        catch (Exception ex)
        {
            return new { Result = "ERROR Reading Database", Message = ex.Message };
        }

    }


    [WebMethod(EnableSession = true)]
    public static object CreateNewDepartment(Department record)
    {
        try
        {
            using (var db = new HRISDBEntities())
            {
                var newTblMember = new tbl_Department
                {
                    Dept_Name = record.Dept_Name

                };
                var NewEmpAdd = db.tbl_Department.Add(newTblMember);
                db.SaveChanges();
                return new { Result = "OK", Records = NewEmpAdd };
            }
        }
        catch (Exception ex)
        {
            return new { Result = "ERROR", Message = ex.Message };
        }
    }




    [WebMethod(EnableSession = true)]
    public static object DeleteDepartment(int record)
    {
        try
        {

            using (HRISDBEntities db = new HRISDBEntities())
            {
                var deletedmember = db.tbl_Department.First(e => e.id == record);
                db.tbl_Department.Remove(deletedmember);
                db.SaveChanges();
                return new { Result = "OK" };

            }
        }
        catch (Exception ex)
        {
            return new { Result = "ERROR", Message = ex.Message };
        }

    }



    [WebMethod(EnableSession = true)]
    public static object UpdateDepartment(Department record)
    {
        try
        {
            int memid =  record.id;
            using (HRISDBEntities db = new HRISDBEntities())
            {
                var updateuser = (from member in db.tbl_Department
                                  where (member.id == memid)
                                  select member).FirstOrDefault();
                updateuser.Dept_Name = record.Dept_Name;

                db.tbl_Department.Attach(updateuser);
                var entry = db.Entry(updateuser);
                entry.Property(e => e.Dept_Name).IsModified = true;

                db.SaveChanges();
                return new { Result = "OK", };
            }
        }
        catch (Exception ex)
        {
            return new { Result = "ERROR", Message = ex.Message };
        }
    }

获得以下错误

Error message

我正在使用Entity Framework 6.x

谢谢!

1 个答案:

答案 0 :(得分:0)

在delete方法中,我需要将参数作为id

传递

更新代码

[WebMethod(EnableSession = true)]
    public static object DeleteDepartment(int id)
    {
        try
        {

            using (HRISDBEntities db = new HRISDBEntities())
            {
                var deletedmember = db.tbl_Department.First(e => e.id == id);
                db.tbl_Department.Remove(deletedmember);
                db.SaveChanges();
                return new { Result = "OK" };

            }
        }
        catch (Exception ex)
        {
            return new { Result = "ERROR", Message = ex.Message };
        }

    }

此删除选项工作后使用Fi9。

感谢。希望它能帮助别人。