MV3 输入验证 - IE8 & IE9 的行为有所不同
我正在使用 DataAnnotations 来验证 MVC3 应用程序上的输入字段。我正在使用正则表达式验证。 我在 IE8 和 IE8 的 UI 上收到验证消息IE9。 但即使在客户端验证失败后,当我点击“保存”按钮时,我还是注意到了差异。 IE9 让我留在客户端。 然而,在 IE8 上,控件转到控制器操作,我必须有一个控制器端 TryValidateModel,以便验证错误。
有谁知道为什么 IE8 会进行服务器往返?
编辑: 添加代码。这进入cshtml。
@using (Html.BeginForm("Person", "Account", FormMethod.Post))
{
<span class="resultError" id="resultError">
@Html.ValidationMessageFor(model => model.Name, "Name should not contain special characters")
</span>
<table>
<tr>
<td class="editor-label">Name:
</td>
<td class="editor-field">@Html.EditorFor(model => model.Name)
</td>
</tr>
</table>
<input type="submit" name="btnKey" value="Save" />
}
这是使用 DataAnnotation 的分部类。 Person 类由 EF 驱动。所以我必须创建一个元数据类来进行验证。
[MetadataType(typeof(personMetadata))]
public partial class person: EntityObject
{
public class personMetadata
{
[Required]
[RegularExpression(@"[A-Za-z0-9]+")]
public object Name { get; set; }
}
}
编辑:添加引用的 javascript 文件。 “~/Scripts/jquery.validate.min.js” “~/Scripts/jquery.validate.unobtrusive.min.js”
I'm using DataAnnotations to validate my input fields on a MVC3 application. I'm using regular expressions validations.
I get the validation messages on the UI for IE8 & IE9.
But I notice the difference when I hit the Save button even after the client side validation has failed.
IE9 keeps me on the client side.
On IE8 however, the control goes to the controller action, and I have to have a controller side TryValidateModel so that the validation errors out.
Does anyone know why IE8 is doing a server round trip?
Edit:
Adding the code. This goes into the cshtml.
@using (Html.BeginForm("Person", "Account", FormMethod.Post))
{
<span class="resultError" id="resultError">
@Html.ValidationMessageFor(model => model.Name, "Name should not contain special characters")
</span>
<table>
<tr>
<td class="editor-label">Name:
</td>
<td class="editor-field">@Html.EditorFor(model => model.Name)
</td>
</tr>
</table>
<input type="submit" name="btnKey" value="Save" />
}
This is the partial class using DataAnnotation. The Person class is driven by EF. So I have to create a metadata class to do the validation.
[MetadataType(typeof(personMetadata))]
public partial class person: EntityObject
{
public class personMetadata
{
[Required]
[RegularExpression(@"[A-Za-z0-9]+")]
public object Name { get; set; }
}
}
Edit: Adding the javascript files that are referenced.
"~/Scripts/jquery.validate.min.js"
"~/Scripts/jquery.validate.unobtrusive.min.js"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我而言,与您的情况很相似,我发现更新 jquery.validate.js 是正确的方法。关于 IE 7 的 jquery 验证版本 1.8.0 有一个报告的错误, 8和9。
获得最新版本后,一切开始工作。
In my case, which is a lot like yours, I found that updating jquery.validate.js was the way to go. There is a reported bug on version 1.8.0 of jquery validation about IE 7, 8 and 9.
After getting the latest version everything started to work.