Asp.Net MVC EnableClientValidation 不起作用

发布于 2024-08-25 19:58:44 字数 1741 浏览 4 评论 0原文

我想要客户端验证和服务器端验证。我意识到这一点如下:

模型:(模型有一个包含测试类的 DataModel(dbml))

namespace MyProject.TestProject
{
    [MetadataType(typeof(TestMetaData))]
    public partial class Test
    {

    }

    public class TestMetaData
    {
        [Required(ErrorMessage="Please enter a name.")]
        [StringLength(50)]
        public string Name { get; set; }
    }
}

控制器没有什么特别的。

视图:

<% Html.EnableClientValidation(); %>
<% using (Ajax.BeginForm("Index", "Test", FormMethod.Post, 
            new AjaxOptions {}, new { enctype = "multipart/form-data" }))
   {%>
   <%= Html.AntiForgeryToken()%>
    <fieldset>
        <legend>Widget Omschrijving</legend>
        <div>
            <%= Html.LabelFor(Model => Model.Name) %>
            <%= Html.TextBoxFor(Model => Model.Name) %>
            <%= Html.ValidationMessageFor(Model => Model.Name) %>
        </div>
    </fieldset>
    <div>
        <input type="submit" value="Save" />
    </div>
 <% } %>

为了使这一切正常工作,我还添加了对 js 文件的引用:

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

最终它必须工作,但它不能 100% 工作: 按下按钮后,它会在没有页面刷新的情况下进行验证。 它还执行“一半”客户端验证。仅当您在文本框中键入一些文本,然后退格所键入的文本时。将出现客户端验证。但是,当我通过在控件之间点击来尝试此操作时,没有客户端验证。

我是否错过了一些参考资料或其他东西? (我使用 Asp.Net MVC 2 RTM)

I want as well as Client Side Validation as Server Side Validation. I realized this as the following:

Model: ( The model has a DataModel(dbml) which contains the Test class )

namespace MyProject.TestProject
{
    [MetadataType(typeof(TestMetaData))]
    public partial class Test
    {

    }

    public class TestMetaData
    {
        [Required(ErrorMessage="Please enter a name.")]
        [StringLength(50)]
        public string Name { get; set; }
    }
}

Controller is nothing special.

The View:

<% Html.EnableClientValidation(); %>
<% using (Ajax.BeginForm("Index", "Test", FormMethod.Post, 
            new AjaxOptions {}, new { enctype = "multipart/form-data" }))
   {%>
   <%= Html.AntiForgeryToken()%>
    <fieldset>
        <legend>Widget Omschrijving</legend>
        <div>
            <%= Html.LabelFor(Model => Model.Name) %>
            <%= Html.TextBoxFor(Model => Model.Name) %>
            <%= Html.ValidationMessageFor(Model => Model.Name) %>
        </div>
    </fieldset>
    <div>
        <input type="submit" value="Save" />
    </div>
 <% } %>

To make this all work I added also references to js files:

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

Eventually it has to work, but it doesnt work 100%:
It does validates with no page refresh after pressing the button.
It also does "half" Client Side Validation. Only when you type some text into the textbox and then backspace the typed text. The Client Side Validation appears. But when I try this by tapping between controls there's no Client Side Validation.

Do I miss some reference or something? (I use Asp.Net MVC 2 RTM)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余罪 2024-09-01 19:58:44

更改加载的 javascript 的顺序...

<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

遇到了完全相同的问题,这为我解决了问题...

Change the order of the loaded javascript...

<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

Had the exact same problem and this cleared it up for me...

小兔几 2024-09-01 19:58:44

我以为我也错过了一些东西,但我确实知道 jQuery 验证插件不会对 mouseEnter/mouseLeave 做出反应。根据该插件的文档(在 插件页面 - 查找标记为“玩演示时需要注意的一些事项”):

在字段被标记为无效之前,验证是惰性的:在第一次提交表单之前,用户可以通过选项卡浏览字段而不会收到烦人的消息 - 在他有机会实际输入之前,他不会被窃听正确的值

我认为 MVC 2 客户端验证的规则相同。我不能确定这一点,因为文档没有说什么,只是“它可以在我的机器上运行”:)。

我选择了 jQuery 验证路线,该路线在 MVC Futures 中受支持 (请注意,该文档不存在)。要使用它,您只需将 MicrosoftAjax.js、MicrosoftMvcAjax.js 和 MicrosoftMvcValidation.js 文件的脚本标记替换为这两个标记:

<script src="/js/jquery.validate.js" type="text/javascript"></script>
<script src="/js/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>

将路径替换为您的路径。

我还发现需要从开发人员页面升级到最新版本的 jquery.validate.js,因为我们在这里使用的是更高版本的 jQuery (1.4)。

希望有帮助。

I thought I was missing something too, but I do know that the jQuery validation plug in doesn't react to mouseEnter/mouseLeave. According to the documentation for that plug in (at the plug in page - look for the section labeled "A few things to look for when playing around with the demo"):

Before a field is marked as invalid, the validation is lazy: Before submitting the form for the first time, the user can tab through fields without getting annoying messages - he won't get bugged before he had the chance to actually enter a correct value

I assume it's the same rule with MVC 2 client-side validation. I can't be sure of that because the documentation doesn't say anything but "it works on my machine" :).

I went with the jQuery validation route, which is supported in MVC Futures (note that the documentation for that is non-existent). To use it, you just need to replace the script tags for MicrosoftAjax.js, MicrosoftMvcAjax.js and MicrosoftMvcValidation.js files with these two tags:

<script src="/js/jquery.validate.js" type="text/javascript"></script>
<script src="/js/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>

replacing the path with yours.

I also found the need to upgrade to the latest version of jquery.validate.js from the developer's page, because we are using a later version of jQuery (1.4) here.

Hope that helps.

叹梦 2024-09-01 19:58:44

嗯...,回答已经晚了,但是试试这个:

<script src="<%= Url.Content("ScriptFile.js") %>" type="text/javascript"></script>

hm..., it's late to answer but try this:

<script src="<%= Url.Content("ScriptFile.js") %>" type="text/javascript"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文