简体   繁体   English

不同的验证消息和验证摘要消息

[英]Different validation message and validation summary message

I'd like to show user different validation messages. 我想向用户显示不同的验证消息。 One is at validation message list, and the other is near my edit box. 一个是验证消息列表,另一个是我的编辑框附近。 Let's see by code: 让我们看看代码:
My Model: 我的型号:

[StringLength(10, ErrorMessage = "{0} cannot be more than {1} characters.")]
string Name { get; set; }

In View: 在视图中:

@* in form *@

@*
My long message is here:
"Name cannot be more than 10 characters."
*@
@Html.ValidationSummary(false)

@*
And my short validation message is here:
"*"
*@
@Html.TextBoxFor( m=> m.Name)
@Html.ValidationMessageFor(m => m.Name)
@*
But this validation message must be "*" instead of "Name cannot be more than 10 characters."
*@

Also I used @Html.ValidationMessageFor(m => m.Name, "ShortMessage") , But didn't worked. 我还使用了@Html.ValidationMessageFor(m => m.Name, "ShortMessage") ,但没有用。 It shows "ShortMessage" as soon as page is loaded. 加载页面后立即显示“ShortMessage”。 Not after the validation failure. 验证失败后没有。

How to acheve this? 怎么搞这个? I'm using MVC4. 我正在使用MVC4。
Any helps would be appreciated. 任何帮助将不胜感激。

Instead of 代替

@Html.ValidationMessageFor(m => m.Name)

Use 采用

@Html.ValidationMessage("Name", "*")

This allows you to specify a message directly in the view, in this case an asterisk. 这允许您直接在视图中指定消息,在本例中为星号。

The reason why site.css is required is because the validation summary and the validation messages are hidden/unhidden with CSS. 需要site.css的原因是因为CSS隐藏/取消隐藏验证摘要和验证消息。 Open up site.css and you will see the following: 打开site.css,您将看到以下内容:

/* styles for validation helpers */
.field-validation-error {
color: #e80c4d;
font-weight: bold;
}

.field-validation-valid {
display: none;
}

input.input-validation-error {
border: 1px solid #e80c4d;
}

input[type="checkbox"].input-validation-error {
border: 0 none;
}

.validation-summary-errors {
color: #e80c4d;
font-weight: bold;
font-size: 1.1em;
}

.validation-summary-valid {
display: none;
}

If you are not using site.css (as in the case I am having, inheriting a project) you will need to bring across these styles. 如果您没有使用site.css(就像我所拥有的那样,继承一个项目),您将需要引入这些样式。

If it is still failing, check your ModelState in the back-end. 如果仍然失败,请检查后端的ModelState。 If the IsValid property reads false, you will see the messages. 如果IsValid属性读为false,您将看到消息。

@Html.ValidationMessageFor(m => m.Name, "*")

This worked after including "Site.css" css file at heading section. 这在包含标题部分的“Site.css”css文件之后起作用。 You must insure "site.css" style classes before using validation. 在使用验证之前,您必须确保“site.css”样式类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM