Asp.Net MVC EnableClientValidation不起作用 [英] Asp.Net MVC EnableClientValidation doesn't work

查看:128
本文介绍了Asp.Net MVC EnableClientValidation不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以及客户端验证服务器端验证。我意识到这一点,如下所示:

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

型号:(该模型包含测试类的DataModel(DBML))

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.

视图:

<% 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文件的引用:

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>

最后,它的工作,但它不工作的100%:
它只会验证以pressing按钮后无刷新页面。
它也确实半壁江山客户端验证。只有当你键入一些文本到文本框,然后退格键,键入的文本。在客户端验证出现。但是,当我尝试这个控件之间攻有没有客户端验证。

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.

我错过了一些参考的东西? (我用Asp.Net MVC 2 RTM)

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

推荐答案

更改装入的javascript的顺序...

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...

这篇关于Asp.Net MVC EnableClientValidation不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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