asp.net-mvc – 客户端验证未显示消息

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 客户端验证未显示消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_0@
我有一个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>
原文链接:https://www.f2er.com/aspnet/246907.html

猜你在找的asp.Net相关文章