MVC 3指定验证触发远程验证 [英] MVC 3 specifying validation trigger for Remote validation

查看:142
本文介绍了MVC 3指定验证触发远程验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了使用MVC 3不显眼的审定如下远程验证

I have implemented remote validation using MVC 3 unobtrusive validation as follows

该模型

public class MyViewModel {
    [Remote("ValidateAction", "Validation")]
    public string Text { get; set; }
}

控制器

public partial class ValidationController : Controller
{
    public virtual JsonResult ValidationAction(string aTextToValidate)
    {
        if(aTextToValidate == /* some condition */)
            return Json(true, JsonRequestBehavior.AllowGet);
        return Json("This is not valid, Try Again !", JsonRequestBehavior.AllowGet);
    }
}

视图

@using (Html.BeginForm() {
    @Html.TextBoxFor(m => m.Text)
    @Html.ValidationMessageFor(m => m.Text)
}

这就是它,它所有的作品,但ValidationAction是在每一个关键preSS,这是不理想的发射,我想就当元素失去专注火,但找不出如何做到这一点。

And that's it, it all works, but ValidationAction is fired on every key press, which is not ideal, I would like just to fire when the element looses focus, but cannot figure how to do this

其实我已经注意到,在默认情况下,确认没有火灾的onblur一次.i.e。首次元素失去焦点,试图纠正然而,当,然后验证触发的onkeyup。我认为这是意功能,并思考它可能是非常有用的。然而这将是有用的知道,如果有可能修改该行为

Actually I have noted that, by default, the validation does fire onBlur once .i.e. the first time the element looses focus, however when attempting to correct it, validation then fires onKeyUp. I think this is intended functionality and thinking about it is probably quite useful. However it would be useful to know if it is possible to modify this behaviour

推荐答案

尝试

$ validator.setDefaults({的onkeyup:假});

这为我工作。

这篇关于MVC 3指定验证触发远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆