ASP。用验证错误消息替换标签?

时间:2022-06-07 04:07:31

I'd like to highlight field names instead of showing a separate error message when there is a validation error.

我希望突出显示字段名,而不是在出现验证错误时显示单独的错误消息。

Is there any convenient way of doing this other than checking the ModelState Errors collection and wrapping each .LabelFor() in an if?

除了检查ModelState错误集合并将每个.LabelFor()包装到if中之外,还有什么其他方便的方法吗?

Also, I'd like to format labels as either bold, or add an asterisk if the model metadata has a [Required] attribute.

此外,我希望将标签格式化为粗体,或者如果模型元数据具有[Required]属性,则添加星号。

1 个解决方案

#1


1  

You'll need use a custom Object.ascx file by creating either /Shared/DisplayTemplates/Object.ascx or /Shared/EditorTemplates/Object.ascx

您需要使用自定义对象。通过创建/共享/显示模板/对象创建ascx文件。ascx或/ / EditorTemplates / Object.ascx共享

I can't answer any better than this guide at Brad Wilson's blog: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

在布拉德·威尔逊(Brad Wilson)的博客(http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom object-templates.html)上,我的答案再好不过了

#1


1  

You'll need use a custom Object.ascx file by creating either /Shared/DisplayTemplates/Object.ascx or /Shared/EditorTemplates/Object.ascx

您需要使用自定义对象。通过创建/共享/显示模板/对象创建ascx文件。ascx或/ / EditorTemplates / Object.ascx共享

I can't answer any better than this guide at Brad Wilson's blog: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

在布拉德·威尔逊(Brad Wilson)的博客(http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom object-templates.html)上,我的答案再好不过了