编辑记录时的jqgrid验证

编辑记录时的jqgrid验证,jqgrid,Jqgrid,我现在在验证正在编辑的记录时遇到问题 编辑表单(和添加表单)有一个名为“电子邮件”的字段。该字段在数据库中应该是唯一的,因此我使用ajax通过以下函数对其进行验证: function check_email(value, colname) { $.ajax({ url: '@Url.Action("CheckEmail")', data: { email: value }, type: 'POST', async: fals

我现在在验证正在编辑的记录时遇到问题

编辑表单(和添加表单)有一个名为“电子邮件”的字段。该字段在数据库中应该是唯一的,因此我使用ajax通过以下函数对其进行验证:

function check_email(value, colname) {
    $.ajax({
        url: '@Url.Action("CheckEmail")',
        data: { email: value },
        type: 'POST',
        async: false,
        datatype: 'text',
        success: function (data) {
            if (!data) result = [true, ""];
            else result = [false, colname + ": ya existe en el sistema"];
        }
    })
    return result;
}
这是可行的,但问题是当我编辑记录时。当然,本例中存在电子邮件,因此它不允许我保存记录

算法可能是:仅当id大于0且输入的电子邮件与现有值不同时,才应进行验证。换句话说,如果用户正在更改电子邮件,则必须进行验证

我花了很多时间试图弄清楚如何知道editform是用于添加还是用于编辑,更重要的是,我试图找到正在编辑的记录的主键的值,但没有成功

任何帮助都将不胜感激

编辑:

这是网格的代码:

    $("#personal").jqGrid({
        url: "@Url.Action("List")",
        datatype: "json",
        mtype: "GET",
        colNames: ["Departamento",
                   "Nombres",
                   "Apellido Paterno",
                   "Apellido Materno",
                   "RUT",
                   "Contraseña",
                   "Fecha Nacimiento",
                   "Fotografía",
                   "Estado Civil",
                   "Género",
                   "Dirección",
                   "Cargo",
                   "E-mail",
                   "Fecha Ingreso",
                   "Creación",
                   "Modificación"],
        colModel: [
            { name: "dep_id", index: "dep_nombre", editable: true, edittype: "select", formatter: 'select', editoptions: { width: 100, value: "@ViewData["Departamentos"]" }, width: 250, editrules: { required: true }, formoptions: { elmsuffix: '<span class="required">*</span>' }, stype: 'select', searchoptions: { sopt: ['eq'], value: "@ViewData["Departamentos"]" } },
            { name: "per_nombres", index: "per_nombres", editable: true, width: 250, editoptions: { maxlength: 80, size: 32 }, editrules: { required: true }, formoptions: { elmsuffix: '<span class="required">*</span>' } },
            { name: "per_apellido_paterno", index: "per_apellido_paterno", editable: true, width: 250, editoptions: { maxlength: 80, size: 32 }, editrules: { required: true }, formoptions: { elmsuffix: '<span class="required">*</span>' } },
            { name: "per_apellido_materno", index: "per_apellido_materno", editable: true, width: 250, editoptions: { maxlength: 80, size: 32 } },
            { name: "per_dni", index: "per_dni", editable: true, width: 100, editoptions: { maxlength: 20, size: 17, dataInit: function (el) { $.mask.definitions['~'] = '[0-9kK]'; $(el).mask("?99.999.999-~"); } }, editrules: { required: true, custom: true, custom_func: is_rut }, formoptions: { elmsuffix: '<span class="required">*</span>' }, searchoptions: { dataInit: function (el) { $.mask.definitions['~'] = '[0-9kK]'; $(el).mask("?99.999.999-~"); } } },
            { name: "per_contrasena", editable: true, edittype: "password", hidden: true, width: 100, editoptions: { maxlength: 50, size: 17 }, editrules: { required: false, edithidden: true } },
            { name: "per_fecha_nacimiento", index: "per_fecha_nacimiento", editable: true, width: 100, editrules: { date: true, required: false }, formatter: 'date', formatoptions: { srcformat: 'SortableDateTime', newformat: 'd-m-Y' }, editoptions: { size: 17, dataInit: function (el) { $(el).datepicker({ dateFormat: 'dd-mm-yy' }); $(el).mask("?99-99-9999"); } }, searchoptions: { dataInit: function (el) { $(el).datepicker({ dateFormat: 'dd-mm-yy' }); $(el).mask("?99-99-9999"); } } },
            { name: "per_fotografia", index: "per_fotografia", editable: true, width: 250, edittype: "file", editoptions: { maxlength: 255, size: 32 } },
            { name: "per_estado_civil", index: "per_estado_civil", editable: true, edittype: "select", editoptions: { value: ":;S:Soltero;C:Casado;V:Viudo;D:Divorciado" }, width: 100, stype: 'select', searchoptions: { sopt: ['eq'], value: ":;S:Soltero;C:Casado;V:Viudo;D:Divorciado" } },
            { name: "per_sexo", index: "per_sexo", editable: true, edittype: "select", editoptions: { value: ":;M:Masculino;F:Femenino" }, width: 100, stype: 'select', searchoptions: { sopt: ['eq'], value: ":;M:Masculino;F:Femenino" } },
            { name: "per_direccion", index: "per_direccion", editable: true, width: 250, editoptions: { maxlength: 512, size: 32 } },
            { name: "per_cargo", index: "per_cargo", editable: true, width: 100, editoptions: { maxlength: 50, size: 32 } },
            { name: "per_email", index: "per_email", editable: true, width: 100, editoptions: { maxlength: 80, size: 32 }, editrules: { email: true, required: false, custom: true, custom_func: check_email } },
            { name: "per_fecha_ingreso", index: "per_fecha_ingreso", editable: true, width: 100, editrules: { date: true, required: false }, formatter: 'date', formatoptions: { srcformat: 'SortableDateTime', newformat: 'd-m-Y' }, editoptions: { size: 17, dataInit: function (el) { $(el).datepicker({ dateFormat: 'dd-mm-yy' }); $(el).mask("?99-99-9999"); } }, searchoptions: { dataInit: function (el) { $(el).datepicker({ dateFormat: 'dd-mm-yy' }); $(el).mask("?99-99-9999"); } } },
            { name: "per_creado_el", index: "per_creado_el", editable:false, search:false, width: 100, align: "center", formatter: "date" },
            { name: "per_modificado_el", index: "per_modificado_el", editable:false, search:false, width: 100, align: "center", formatter: "date" },
        ],
        jsonReader: {
            repeatitems: false,
            id: "per_id"
        },
        pager: "#pager",
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: "per_apellido_paterno",
        sortorder: "asc",
        viewrecords: true,
        gridview: true,
        autoencode: true,
        multiselect: true,
        shrinkToFit: false,
        caption: "Funcionarios",
        editurl: "@Url.Action("AjaxEdit")",
        height: '100%',
        width: 935,
        rownumbers: true,
        rownumWidth: 40            
    });

    $("#personal").jqGrid('hideCol', ["per_fotografia", "per_direccion"]);
    $("#personal").jqGrid('navGrid', '#pager', { edit: true, add: true, del: true, search: false }, { width: 500 }, { width: 500 }, {}, { multipleSearch: false, multipleGroup: false });
    $("#personal").jqGrid('filterToolbar', { searchOperators: false });

    $.jgrid.edit.addCaption = "Agregar Funcionario";
    $.jgrid.edit.editCaption = "Modificar Funcionario";
    $.jgrid.edit.saveData = "¡El funcionario fue modificado! ¿Almacena los cambios?";

    $.jgrid.formatter.date.newformat = 'd-m-Y H:i';
$(“#个人”).jqGrid({
url:“@url.Action(“列表”)”,
数据类型:“json”,
mtype:“获取”,
colNames:[“departmento”,
“名义”,
“阿佩利多·帕特诺”,
“阿佩利多马特诺”,
“发情”,
“反邪教”,
“Fecha Naciminto”,
“Fotografía”,
“Estado Civil”,
“Género”,
“Dirección”,
“货物”,
“电子邮件”,
“Fecha Ingreo”,
“克雷西翁”,
“修改”],
colModel:[
{name:“dep_id”,index:“dep_nombre”,edittype:“select”,formatter:'select',editoptions:{width:100,value:@ViewData[“departmentos”]”},width:250,editrules:{required:true},formoptions:{elmsuffix:'*},stype:'select',searchoptions:{sopt:['eq'],value:@ViewData[“departmentos”},
{name:“per_nombres”,index:“per_nombres”,可编辑:true,width:250,editoptions:{maxlength:80,size:32},editrules:{required:true},formoptions:{elmsuffix:'*},
{name:“per_apellido_paterno”,index:“per_apellido_paterno”,可编辑:true,宽度:250,编辑选项:{maxlength:80,大小:32},编辑规则:{required:true},formoptions:{elmsuffix:'*},
{name:“per_apellido_materno”,index:“per_apellido_materno”,可编辑:true,宽度:250,编辑选项:{maxlength:80,大小:32},
{name:“per_dni”,index:“per_dni”,可编辑:true,宽度:100,editoptions:{maxlength:20,大小:17,dataInit:function(el){$.mask.definitions['~']='[0-9kK]';$(el)。mask(“?99.999.999-~”);},editrules:{必需:true,custom:true,custom:true,custom_函数:is_rut},formoptions:{elmsuffix:'*},searchoptions:{dataInit:function(el){$.mask.definitions['~']='[0-9kK]';$(el.mask(“?99.999.999-~”);},
{name:“per_contasena”,可编辑:true,edittype:“password”,hidden:true,width:100,editoptions:{maxlength:50,size:17},editrules:{required:false,edithidden:true},
{name:“per_fecha_nacimiento”,index:“per_fecha_nacimiento”,可编辑:true,宽度:100,编辑规则:{date:true,required:false},格式化程序:'date',格式化选项:{srcformat:'SortableDateTime',newformat:'d-m-Y',editoptions:{size:17,dataInit function(el){$(el).日期选择器({dateFormat:'dd mm-yy'});$(el).掩码(“?99-9999”)}},searchoptions:{dataInit:function(el){$(el).datepicker({dateFormat:'dd-mm-yy'});$(el).mask(“?99-99-9999”);}},
{name:“peru fotografia”,index:“peru fotografia”,可编辑:true,宽度:250,edittype:“file”,editoptions:{maxlength:255,size:32},
{name:“peru estado_civil”,index:“peru estado_civil”,可编辑:true,edittype:“select”,editoptions:{value:”;S:Soltero;C:Casado;V:Viudo;D:Divorciado“},宽度:100,stype:'select',searchoptions:{sopt:['eq'],value:;S:Soltero;C:Casado;V:Viudo;D:Divorciado”},
{name:“peru sexo”,index:“peru sexo”,edittype:“true”,edittype:“select”,editoptions:{value::;M:mangino;F:Femenino”},宽度:100,stype:'select',searchoptions:{sopt:['eq'],value::;M:mangino;F:Femenino},
{name:“peru direcion”,index:“peru direcion”,可编辑:true,width:250,editoptions:{maxlength:512,size:32},
{名称:“每件货物”,索引:“每件货物”,可编辑:true,宽度:100,编辑选项:{maxlength:50,大小:32},
{name:“per_email”,index:“per_email”,可编辑:true,宽度:100,编辑选项:{maxlength:80,大小:32},编辑规则:{email:true,必需:false,自定义:true,自定义函数:check_email},
{name:“per_fecha_ingreso”,index:“per_fecha_ingreso”,可编辑:true,宽度:100,编辑规则:{date:true,required:false},格式化程序:'date',格式化选项:{srcformat:'SortableDateTime',newformat:'d-m-Y',编辑选项:{size:17,dataInit function(el){$(el).日期选择器({dateFormat:'dd mm-yy'});$(el).mask(“?99-9999”)}},searchoptions:{dataInit:function(el){$(el).datepicker({dateFormat:'dd-mm-yy'});$(el).mask(“?99-99-9999”);}},
{名称:“per_creado_el”,索引:“per_creado_el”,可编辑:false,搜索:false,宽度:100,对齐:“中心”,格式化程序:“日期”},
{name:“per_modificado_el”,index:“per_modificado_el”,可编辑:false,搜索:false,宽度:100,对齐:“中心”,格式化程序:“日期”},
],
jsonReader:{
重复项:false,
id:“每个id”
},
寻呼机:“#寻呼机”,
rowNum:10,
行列表:[10,20,30],
sortname:“per_apellido_paterno”,
分拣员:“asc”,
viewrecords:是的,
gridview:没错,
自动编码:正确,
多选:对,
shrinkToFit:错,
标题:“Functionarios”,
editurl:“@Url.Action”(“AjaxEdit”)”,
function check_email(value, colname) {

    var rowid = jQuery('#personal').jqGrid('getGridParam', 'selrow');

    $.ajax({
        url: '@Url.Action("CheckEmail")',
        data: { email: value, rowid: rowid },
        type: 'POST',
        async: false,
        datatype: 'text',
        success: function (data) {
            if (!data) result = [true, ""];
            else result = [false, colname + ": ya existe en el sistema"];
        }
    })
    return result;
}