设置 MVC3 验证消息的样式
我喜欢 MVC3 中的默认验证消息,但现在我正在转向一些更大的表单,表单元素需要更紧密地组合在一起。这会导致生成的默认验证消息出现问题。它们要么换行,导致一半消息出现在表单元素下,要么完全破坏我的表单布局。
有没有人有一个好的解决方案来解决这个问题?
您认为将验证消息设置为“工具提示”会有帮助吗?很想听听您的建议和解决方案。
问候保罗
I love the default validation messages in MVC3 but now I am moving onto some much larger forms the form elements need to sit a lot tighter together. This causes issues with the default validation messages produced. They either wrap, leading to half the message appearing under the form element, or break my form layout completely.
Does anyone have a good solution to this problem?
Would setting the validation message to being a 'tooltip' help do you think? Would love to hear your suggestions and solutions.
Regards Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认验证消息是
SPAN
。如果您想使用不同的元素(例如IMG
),则需要创建自定义验证消息扩展。查看 MVC 中的 ValidationMessageHelper 方法,了解默认的 SPAN 是如何构建的。我认为您可以重用data-valmsg-for
等自定义属性。另外,假设您使用的是非侵入式 ajax 验证,则需要修改jquery.validate.unobtrusive.js。The default validation message is a
SPAN
. If you want to use a different element (e.g.IMG
), you would need to create a custom validation message extension. Check outValidationMessageHelper
method in MVC to get an idea how the defaultSPAN
is built. I think you can reuse the custom attributes likedata-valmsg-for
. Also, you need to modify the jquery.validate.unobtrusive.js assuming you are using unobtrusive ajax validation.这完全取决于您的布局。您可以将 ValidationMessage 放在您想要的任何位置,并按照您想要的方式设置其样式。只需查看输出 HTML 并弄清楚如何按照您想要的方式布局消息即可。
This is all dependant upon your layout. You can put the ValidationMessage anywhere you want, and style it any way you want. Just look at the output HTML and figure out how to lay out the message how you want it.