缺少链接MVC客户端验证某处 [英] Missing link somewhere for MVC client-side validation

查看:131
本文介绍了缺少链接MVC客户端验证某处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的客户端验证在MVC的项目,但我使用它的方式是有点复杂,所以我想我已经错过了一些东西的地方,是在你需要的东西链造成破建立以它的工作。

I'm using client-side validation in an MVC project, but the way I'm using it is a little complicated and so I think I've missed something somewhere that is causing a break in the chain of things you need to set up in order for it to work.

据我了解,这是完整的画面:

As I understand it, this is the complete picture:


  1. 您需要具备以下JS库加载:jQuery的,jQuery的验证,以及微软不显眼的审定

  2. 您必须启用客户端验证,和不显眼的验证,在Web.config。

  3. 您需要应用一些验证属性模型。

  4. 您需要包括一些用于显示视图中的错误。

我相信我所做的这一切,但也有复杂的事情我特解的一些地方,也许有人可以帮我整理一下。

I believe I've done all this, but there are some parts of my particular solution that complicate things, and maybe someone can help me sort it out.

在我而言,我已经包括了所有的JS库。我检查了,我的Web.config文件看起来像这样:

In my case, I've included all of the JS libraries. I've checked that my Web.config looks like this:

<configuration>
    [...]
    <appSettings>
        [...]
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
        [...]
    </appSettings>
    [...]
</configuration>

我已经申请了一些验证属性我的模型:

I have applied some validation attributes to my model:

public class CreateStudentEventViewModel : IValidatableObject
{
    [Required]
    public DateTime StartDate { get; set; }
}

和我已经包含在我看来,验证结果显示:

And I have included a display for the validation results in my view:

@using (Html.BeginForm([...]))
{
    [...]
    @Html.ValidationSummary()
    [...]
}

然而,当我点击提交按钮,一个正常的提交发生。我怀疑这是因为如何,我管理的形式提交。我通过AJAX实际加载的形式进入一个模式。我怀疑这是怎么回事是,因为形式并不在页面加载的DOM存在,MVC是不重视它的正确提交监听器的形式。

However, when I click the submit button, a normal submit occurs. I suspect this is because of how I'm managing form submits. I'm actually loading the form via AJAX into a modal. I suspect what's happening is that because the form doesn't exist in the DOM on page load, MVC isn't attaching its submit listener to the form correctly.

因此​​,事不宜迟,我的问题是:

So without further ado, my question is:

有没有办法来手动附加不显眼的审定提交听众或手动触发不显眼的验证?

我是在错误的轨道上,和别的东西是错的?

推荐答案

您还没有表现出你包含的文件,但他们需要 jQuery的。{}版本的.js jquery.validate.js jquery.validate.unobtrusive.js

You have not shown the files you included, but they need to be jquery.{version}.js, jquery.validate.js and jquery.validate.unobtrusive.js.

但问题是,你添加动态内容到页面。当第一次加载页面, jquery.validate.unobtrusive 解析窗体控件,并生成验证规则的每个元素。如果你添加更多表单控件到DOM,需要让客户端验证可以应用到这些控件重新解析验证。

But the problem is that your adding dynamic content to the page. When the page is first loaded, jquery.validate.unobtrusive parses the forms controls and generates the validation rules for each element. If you add further form controls to the DOM, you need to re-parse the validator so client side validation can be applied to those controls.

$('form').data('validator', null);
$.validator.unobtrusive.parse($('form'));

这篇关于缺少链接MVC客户端验证某处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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