简体   繁体   English

ASP.NET MVC 3不引人注目的验证 - 在验证事件之前?

[英]ASP.NET MVC 3 Unobtrusive Validation - Before Validate Event?

Using ASP.NET MVC3's unobtrusive validation feature how can I run some JavaScript before validation runs? 使用ASP.NET MVC3的不显眼的验证功能如何在验证运行之前运行一些JavaScript? Jquery's submit handler runs after validation for obvious reasons. 出于显而易见的原因,Jquery的提交处理程序在验证后运行。 I can hook into a click on the submit button but that doesn't seem very elegant. 我可以点击提交按钮,但这看起来并不优雅。 Any suggestions? 有什么建议么?

EDIT 编辑

It appears my above statement is incorrect. 看来我的上述陈述是不正确的。 As was pointed out below the submit handler does run before validation. 如下所述,提交处理程序确实在验证之前运行。 Unfortunately that does not solve my problem. 不幸的是,这并没有解决我的问题。

My revised question: 我修改过的问题:

I need to manipulate a form value after form submission but before jQuery validation. 我需要在表单提交之后但在jQuery验证之前操作表单值。 If I change the value after submission using jQuery's submit handler then jQuery validates the original value not the new value. 如果我在使用jQuery的提交处理程序提交后更改值,则jQuery验证原始值而不是新值。

The submit handler doesn't run after validation. 验证后,提交处理程序不会运行。 It runs before. 它之前运行。 Try it: 试试吧:

$('form').submit(function () {
    alert('validation hasn\'t run yet at this point => see there is no red color over your form input fields yet');

    if ($(this).valid()) {
        alert('the form is valid');
    } else {
        alert('the form is not valid');
    }
});

UPDATE: 更新:

After the revised question I am still unable to reproduce the issue. 在修改后的问题之后,我仍然无法重现这个问题。 Here's my model: 这是我的模特:

public class MyModel
{
    [Required]
    public string Name { get; set; }
}

and here's my view: 这是我的看法:

@model MyModel

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>           

@using (Html.BeginForm(null, null, FormMethod.Post, new { }))
{
    @Html.EditorFor(x => x.Name)
    @Html.ValidationMessageFor(x => x.Name)
    <input type="submit" value="OK" />
}

<script type="text/javascript">
    $('form').submit(function () {
        $('#Name').val('some value');
    });
</script>

Now if I leave the Name field blank and submit the form, the new value is being properly assigned and the form is successfully submitted to the server without any errors. 现在,如果我将“名称”字段留空并提交表单,则会正确分配新值,并且表单已成功提交给服务器而没有任何错误。 If I remove the $('#Name').val('some value'); 如果我删除$('#Name').val('some value'); line that assigns a new value and try to submit the form with an empty field client side validation triggers and the form is not submitted. 分配新值并尝试使用空字段客户端验证触发器提交表单的行,并且不提交表单。

I'm not sure if it will be a solution but you could have a look to IClientValidatable Interface http://msdn.microsoft.com/en-us/library/system.web.mvc.iclientvalidatable(v=vs.98).aspx 我不确定它是否是一个解决方案,但你可以查看IClientValidatable Interface http://msdn.microsoft.com/en-us/library/system.web.mvc.iclientvalidatable(v=vs.98)的.aspx

Here's example (quite old) but should work in the current release: ASP.NET MVC: Adding client-side validation to ValidatePasswordLengthAttribute in ASP.NET MVC 3 Preview http://blogs.msdn.com/b/stuartleeks/archive/2010/07/28/asp-net-mvc-adding-client-side-validation-to-validatepasswordlengthattribute-in-asp-net-mvc-3-preview-1.aspx 这是一个例子(相当古老)但应该在当前版本中工作:ASP.NET MVC:在ASP.NET MVC 3预览中向ValidatePasswordLengthAttribute添加客户端验证http://blogs.msdn.com/b/stuartleeks/archive/2010 /07/28/asp-net-mvc-adding-client-side-validation-to-validatepasswordlengthattribute-in-asp-net-mvc-3-preview-1.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM