C# 如何使用 html.ValidationMessageFor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19281415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use html.ValidationMessageFor
提问by SantasNotReal
I'm trying to get my view to give me the error message next to the text box if a user enters something invalid (like a string where it's expecting a number). Instead, I'm getting an ugly error page saying validation failed when the user presses submit.
如果用户输入无效的内容(例如需要数字的字符串),我试图让我的视图在文本框旁边给我错误消息。相反,我收到一个丑陋的错误页面,说当用户按下提交时验证失败。
Here is a portion of my view:
以下是我的部分观点:
@model MembershipTest.ViewModels.AddDriverViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Add Drivers";
}
@using (Html.BeginForm("AddDriver", "Driver"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Customer Information</legend>
<table>
<tr>
<td>
@Html.Label("First Name:")
@Html.TextBoxFor(m => m.Driver.F_Name)
@Html.ValidationMessageFor(m => m.Driver.F_Name)
</td>
<td>
@Html.Label("Gender:")
@Html.RadioButtonFor(m => m.isMaleChecked, "true") Male
@Html.RadioButtonFor(m => m.isMaleChecked, "false")Female
</td>
</tr>
<tr>
<td>
@Html.Label("Last Name:")
@Html.TextBoxFor(m => m.Driver.L_Name)
@Html.ValidationMessageFor(m => m.Driver.L_Name)
</td>
And here is the relevant portion of my model:
这是我的模型的相关部分:
[Required]
[StringLength(30)]
public string F_Name { get; set; }
[Required]
[StringLength(30)]
public string L_Name { get; set; }
In the post method of my controller, I make sure to use
在我的控制器的 post 方法中,我确保使用
if (ModelState.IsValid)
If the user happened to enter something like 50 characters long in the first name text box, I want an error to be displayed using the Html.ValidationMessageFor() right when they tab out of that text box, so they can see it before they press submit. Am I missing some jquery to make this happen? Maybe some using statement I need to include?
如果用户碰巧在名字文本框中输入了 50 个字符之类的内容,我希望在他们退出该文本框时使用 Html.ValidationMessageFor() 显示错误,以便他们可以在按下之前看到它提交。我是否缺少一些 jquery 来实现这一点?也许我需要包含一些 using 语句?
采纳答案by SantasNotReal
It was silly simple.....I just didn't add the ErrorMessage field as part of the [Required] decorator. For example:
这是愚蠢的简单.....我只是没有添加 ErrorMessage 字段作为 [Required] 装饰器的一部分。例如:
[Required(ErrorMessage = "First name is required")]
[StringLength(30, ErrorMessage = "Name can be no larger than 30 characters")]
public string F_Name { get; set; }
[Required(ErrorMessage = "Last name is required")]
[StringLength(30, ErrorMessage = "Name can be no larger than 30 characters")]
public string L_Name { get; set; }
Now, if a user either doesn't enter anything in the name fields, or enters something over 30 characters, the Post method doesn't get executed and the user gets a little message telling them what's wrong.
现在,如果用户没有在名称字段中输入任何内容,或者输入了超过 30 个字符的内容,则不会执行 Post 方法,并且用户会收到一条小消息,告诉他们出了什么问题。