.Net Mvc 3 触发器(提交按钮除外)不显眼的验证 [英] .Net Mvc 3 Trigger (other than submit button) Unobtrusive Validation

查看:18
本文介绍了.Net Mvc 3 触发器(提交按钮除外)不显眼的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要什么

我想使用我选择的事件在我的视图中触发客户端验证.它可能是onblur",可能是另一个按钮,但不是提交按钮.

I would like to trigger client-side validation in my View with an event of my choice. It could be 'onblur' maybe another button but something other than the submit button.

相关链接

如何在不使用提交按钮的情况下触发验证

对动态内容应用不显眼的 jquery 验证在 ASP.Net MVC 中

我的尝试

给定了各种事件侦听器,我在没有运气的情况下触发了以下方法:

Given a various event listener, I've fired the following methods with no luck:

$(selector).validate();

$(selector).valid();

$.validator.unobtrusive.parseDynamicContent(selector);

$.validator.unobtrusive.parse($(selector));

总结

所以我需要客户端验证来触发给定的事件(提交除外)并显示相应的验证消息.我不觉得任何标记/剃刀语法是必要的,因为客户端验证会在提交时触发,并且所有相应的验证消息都按预期显示.

So I need client-side validation to fire on a given event (other than on submit) and show the corresponding validation messages. I dont feel like any of the Markup/Razor syntax is necessary as client-validation fires on submit and all the corresponding validation messages show as expected.

推荐答案

$('form').valid() 应该可以工作.让我们举例说明.

$('form').valid() should work. Let's exemplify.

型号:

public class MyViewModel
{
    [Required]
    public string Foo { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }
}

查看:

@model MyViewModel

<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())
{
    @Html.LabelFor(x => x.Foo)
    @Html.EditorFor(x => x.Foo)
    @Html.ValidationMessageFor(x => x.Foo)
}

<div id="validate">Hover here to trigger validation</div>

<script type="text/javascript">
    $('#validate').hover(function () {
        $('form').valid();
    });
</script>

这篇关于.Net Mvc 3 触发器(提交按钮除外)不显眼的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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