Ajax验证在jquery验证引擎中无效

时间:2014-06-23 11:35:07

标签: jquery ajax asp.net-mvc jquery-validation-engine

我是jquery验证引擎的新手,并在我的asp.net mvc应用程序中使用它来验证ItemNo。我在我的视图中使用以下代码:

@Html.TextBoxFor(model => model.ItemNo, new { @class="validate[required,ajax[ajaxCheckItemNo]]" })

我的jquery.validationEngine-en.js中的代码

"ajaxCheckItemNo": {
                // remote json service location
                "url": "/DrugManager/IsItemNoAlreadyExists",
                "extraDataDynamic": ['#ItemNo'],
                // error
                "alertText": "* This name is already taken",
                // if you provide an "alertTextOk", it will show as a green prompt when the field validates
                "alertTextOk": "* This name is available",
                // speaks by itself
                "alertTextLoad": "* Validating, please wait"
            }

并在我的控制器中跟随代码

[HttpGet]
    public JsonResult IsItemNoAlreadyExists(string ItemNo, int DrugID = 0, bool IsAddForm = true)
    {
        bool? ReturnValue = null;

        try
        {
            /* returns true/false */
            ReturnValue = ObjDrugManager.IsItemNoAlreadyExists(ItemNo, DrugID, IsAddForm);
        }
        catch (Exception ex)
        {
            /* Catch Exception Here */                
        }

        return Json(new { ReturnValue });

当我运行代码时,我的服务器代码也会返回值但是在我的视图中只显示加载消息

enter image description here

2 个答案:

答案 0 :(得分:0)

        public JsonResult IsItemNoAlreadyExists(string ItemNo, int DrugID = 0, bool IsAddForm = true)
        {
            bool? ReturnValue = null;
     ArrayList validationResult = new ArrayList();
            validationResult.Add("ItemNo");
            try
            {
                /* returns true/false */
                ReturnValue = ObjDrugManager.IsItemNoAlreadyExists(ItemNo, DrugID, IsAddForm);

 if(ReturnValue ==false)
            {
                validationResult.Add(ReturnValue );
                validationResult.Add("Error message.");
            }
            else{
                validationResult.Add(ReturnValue);
                validationResult.Add("success message");
            }       
            }
            catch (Exception ex)
            {
                /* Catch Exception Here */                
            }

            return Json(validationResult, JsonRequestBehavior.AllowGet });
    }

答案 1 :(得分:0)

我通过在ajax响应中以这种格式返回json来解决它。

["field_id",true]