ASP.NET MVC RemoteAttribute validation not working - Action not being executed

最后都变了- 提交于 2019-12-04 08:52:06
parliament

Ok I found my answer.

To quote Darin Dimitrov here:

"Unobtrusive validation doesn't work out-of-the-box with dynamically added elements to the DOM - such as for example sending an AJAX request to the server which returns a partial view and this partial view is then injected into the DOM.

In order to make it work you need to register those newly added elements with the unobtrusive validation framework. To do this you need to call the $.validator.unobtrusive.parse on the newly added elements. You should put this code inside the AJAX success handler that is injecting the partial into your DOM."

The framework calls this method one time on page load, the problem in my scenario was that the form itself was a jquery dialog and so even the initial load was "dynamic". I had to register the form elements with the unobtrusive framework on dialog load:

$.validator.unobtrusive.parse('#registerForm');  

My ajax calls would also return and replace the form like so if it did not validate on the server-side:

registerDialog.empty().html(result.viewResult);

so I had to call parse() on the success callback as well to make sure validation continues to work after an ajax submit.

You need to change your JsonResult method parameter name from

public JsonResult IsUserNameAvailable(string username)

to

public JsonResult IsUserNameAvailable(string UserName)

it should be same as property name its case-sensitive.

Esteban Elverdin

The problem why validation function is not being fired is that this code

public JsonResult IsUserNameAvailable(string username)
{
    User user = userRepository.Get(u => u.UserName == username);

    if (user == null) return Json(true, JsonRequestBehavior.AllowGet);
    else return Json(false, JsonRequestBehavior.AllowGet);
}

should be added to the AccountController, not the ValidationController. Remove ValidationController class and add the IsUserNameAvailable method to your AccountController class.

Also change the line in the model, it should be something like this

[System.Web.Mvc.Remote("IsUserNameAvailable", "Account")]

That will solve the issue.

It seems you forget to specify the binding Action for the form. I tried this below code and it works

<form id="registerForm" action="Controller/Action">
     @Html.ValidationMessageFor(m => m.UserName)
     @Html.TextBoxFor(m => m.UserName)
     @Html.LabelFor(m => m.UserName)
</form>

or simply:

@using(Html.BeginForm()) {
     @Html.ValidationMessageFor(m => m.UserName)
     @Html.TextBoxFor(m => m.UserName)
     @Html.LabelFor(m => m.UserName)
}
Aurora

I had kind of the same issue. Simply change this:

public JsonResult IsUserNameAvailable(string username)
{
    User user = userRepository.Get(u => u.UserName == username);

    if (user == null) return Json(true, JsonRequestBehavior.AllowGet);
    else return Json(false, JsonRequestBehavior.AllowGet);
}

to:

public JsonResult IsUserNameAvailable(string Username)
{
    User user = userRepository.Get(u => u.UserName == Username);
    if (user == null) 
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }
    return Json(false, JsonRequestBehavior.AllowGet);       
}

username to UserName which is your

public string UserName { get; set; }

I think it's case sensitive.

Hope it will help :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!