ASP.NET MVC RemoteAttribute验证不工作 - 行动未执行 [英] ASP.NET MVC RemoteAttribute validation not working - Action not being executed
问题描述
我一直在拉我的头发试图找出为什么ValidationController操作不会被触发。
I've been pulling my hair out trying to figure out why ValidationController actions are not being triggered.
我在项目范围内的web.config中启用设置:
I have settings enabled in project-wide web.config:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
我有以下控制器:
I have the following controller:
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public class ValidationController : Controller
{
private readonly IUserRepository userRepository;
public ValidationController(IUserRepository userRepository)
{
this.userRepository = userRepository;
}
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);
}
}
和以下视图模型:
[Required(ErrorMessage = "Required *")]
[StringLength(50, MinimumLength = 4, ErrorMessage = "Please keep the username between four and twenty character.")]
[Remote("IsUserNameAvailable", "Validation", ErrorMessage = "A user with this username already exists.")]
[Display(Name = "Username")]
public string UserName { get; set; }
和我有以下几个领域在我的方式:
And I have the following field in my form:
<form id="registerForm">
...
@Html.ValidationMessageFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.LabelFor(m => m.UserName)
</form>
我做的Ajax表单提交,并且已拥有服务器端的验证工作完美:
I do ajax form submission and already have server-side validation working perfectly:
$.post("/Account/Register", $('#registerForm').serialize(), function(){
updateFormWithServerGeneratedErrors();
})
虽然服务器生成正确的输入标签为我场:
Although the server generated the correct input tag for my field:
<input ... data-val-remote-url="/Validation/IsUserNameAvailable" data-val-remote-additionalfields="*.UserName" data-val-remote="A user with this username already exists." ... >
我可以手动打我的行动通过键入到URL:/确认/ IsUserNameAvailable用户名= SomeName,但菲德勒不显示任何请求被做这个网址在关键presses或重点的变化。
I can manually hit my action by typing into the url: "/Validation/IsUserNameAvailable?username=SomeName" but Fiddler is NOT showing any requests being made to this url on keypresses or focus changes.
根据本教程我不需要写任何javascript来得到这个工作。是的,我有jquery.validate.js和jquery.validate.unobtrusive.js脚本已经被我先从形式修修补补时加载的。
According to this tutorial I don't need to write any javascript to get this working. And yes, I have jquery.validate.js and jquery.validate.unobtrusive.js scripts already loaded by the time I start tinkering with the form.
这里有什么问题吗?
推荐答案
好吧,我发现我的答案。
Ok I found my answer.
要在这里引述达林季米特洛夫:
To quote Darin Dimitrov here:
不显眼的验证不工作外的开箱即用的动态添加元素的DOM - 那么例如发送一个Ajax请求返回的局部视图,这部分观点的服务器注入DOM。
"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.
为了使它工作,你需要注册那些不显眼的验证框架新添加的元素。要做到这一点,你需要调用新添加的元素$ .validator.unobtrusive.parse。你应该把被注射局部到您的DOM的AJAX成功处理这里面code。
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."
框架调用这个方法一个在页面加载时间,在我的情况的问题是,表格本身是一个jQuery对话框,因此,即使初始加载是动态的。我必须注册在对话框负载不显眼的框架表单元素:
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');
我的Ajax调用也将返回和替换像这样的形式,如果它没有在服务器端验证:
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);
所以我不得不呼吁成功回调解析(),以及确保验证继续工作一个ajax后提交
so I had to call parse() on the success callback as well to make sure validation continues to work after an ajax submit.
这篇关于ASP.NET MVC RemoteAttribute验证不工作 - 行动未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!