gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:11:35 24 4
gpt4 key购买 nike

我有一个带有用于创建用户帐户的表单的 MVC4 Internet 应用程序。表单验证有效,但当输入验证失败时,不会显示错误消息。它仍然阻止提交,直到验证问题解决但没有文本

Razor View 表单

<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*"));

使用的Model是实体模型,我添加了一个partial类来注释验证需求
[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>

关于asp.net-mvc - 客户端验证未显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17191459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com