mercredi 6 mai 2015

Remote validation is not working

When I entered value for email in textbox it didn't fire VerifyUserEmail method. Below is my code. Where is my mistake?

View

<div class="form-group">
    @Html.LabelFor(model => model.User_Email, new { @class = "control-label col-lg-2" })
        <div class="col-lg-4">
            @Html.EditorFor(model => model.User_Email)
            @Html.ValidationMessageFor(model => model.User_Email)
        </div>
</div>

Model

[DisplayName("Email")]       
[Remote("VerifyUserEmail", "User", ErrorMessage = "Email already exists!")]
public string User_Email { get; set; }

Controller

    public JsonResult VerifyUserEmail(string User_Email)
    {
        try
        {
            using (RKDYMEntities objConnection = new RKDYMEntities())
            {
                ObjectParameter objIErrorCode = new ObjectParameter("ErrorCode", typeof(Int32));
                ObjectParameter objBFlag = new ObjectParameter("bFlg", typeof(bool));
                objConnection.Check_User_Exists(User_Email, objBFlag, objIErrorCode);

                if (Convert.ToBoolean(objBFlag.Value) != true)
                {
                    return Json(false, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(true, JsonRequestBehavior.AllowGet);
                }


            }
        }
        catch (Exception Ex)
        {
            return Json(false, JsonRequestBehavior.AllowGet);
        }
    }

I have also added <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> in web.config.

Below is generated html code:

 <div class="form-group">
    <label class="control-label col-lg-2" for="User_Email">Email</label>
        <div class="col-lg-4">
            <input class="text-box single-line" data-val="true" data-val-length="The field Email must be a string with a maximum length of 200." data-val-length-max="200" data-val-remote="Email already exists!" data-val-remote-additionalfields="*.User_Email" data-val-remote-url="/User/VerifyUserEmail" id="User_Email" name="User_Email" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="User_Email" data-valmsg-replace="true"></span>
       </div>
 </div>

Aucun commentaire:

Enregistrer un commentaire