我有一个MVC4互联网应用程序,其中包含用于创建用户帐户的表单.表单验证有效,但输入验证失败时,不会显示错误消息.它仍然会阻止提交,直到验证问题得到解决但没有文本
剃刀查看表格
<h2>Create New Account</h2> <fieldset> <legend></legend> @using (Html.BeginForm("CreateUser",null)){ @Html.AntiForgeryToken() <table class="create"> <tr> <td colspan="2"><b>New Account</b> </tr> <tr> <td>@Html.DisplayNameFor(model=>model.UserName)</td><td>@Html.TextBoxFor(model=>model.UserName)</td> <td>@Html.DisplayNameFor(model=>model.EmailAddress)</td><td>@Html.TextBoxFor(model=>model.EmailAddress)</td> <td><input type="submit" value="Create User" /></td> </tr> </table> } </fieldset> @Html.ValidationSummary()
bundles.Add(new ScriptBundle("~/bundles/asset").Include( "~/Scripts/jquery-{version}.js","~/Scripts/jquery-ui-{version}.js","~/Scripts/jquery.validate*","~/Scripts/jquery.unobtrusive*"));
[MetadataType(typeof(UserProfileMetadata))] public partial class UserProfile { //Empty Class just required for adding class level attribute } public class UserProfileMetadata { //Fields from user profile requiring annotations [EmailAddress] [required] [Display(Name = "Email Address")] public string EmailAddress { get; set; } [required] public string UserName { get; set; } }
解决方法
在表单中移动ValidationSummary将修复它.
<h2>Create New Account</h2> <fieldset> <legend></legend> @using (Html.BeginForm("CreateUser",null)){ @Html.ValidationSummary() @Html.AntiForgeryToken() <table class="create"> <tr> <td colspan="2"><b>New Account</b> </tr> <tr> <td>@Html.DisplayNameFor(model=>model.UserName)</td> <td>@Html.TextBoxFor(model=>model.UserName)</td> <td>@Html.DisplayNameFor(model=>model.EmailAddress)</td><td>@Html.TextBoxFor(model=>model.EmailAddress)</td> <td><input type="submit" value="Create User" /></td> </tr> </table> } </fieldset>