Asp.Net MVC EnableClientValidation 不起作用
我想要客户端验证和服务器端验证。我意识到这一点如下:
模型:(模型有一个包含测试类的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改加载的 javascript 的顺序...
遇到了完全相同的问题,这为我解决了问题...
Change the order of the loaded javascript...
Had the exact same problem and this cleared it up for me...
我以为我也错过了一些东西,但我确实知道 jQuery 验证插件不会对 mouseEnter/mouseLeave 做出反应。根据该插件的文档(在 插件页面 - 查找标记为“玩演示时需要注意的一些事项”):
我认为 MVC 2 客户端验证的规则相同。我不能确定这一点,因为文档没有说什么,只是“它可以在我的机器上运行”:)。
我选择了 jQuery 验证路线,该路线在 MVC Futures 中受支持 (请注意,该文档不存在)。要使用它,您只需将 MicrosoftAjax.js、MicrosoftMvcAjax.js 和 MicrosoftMvcValidation.js 文件的脚本标记替换为这两个标记:
将路径替换为您的路径。
我还发现需要从开发人员页面升级到最新版本的 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"):
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:
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.
嗯...,回答已经晚了,但是试试这个:
hm..., it's late to answer but try this: