我想在视图中为 Html.ValidationMessageFor 定义 HTML 结构。这应该类似于 EditorTemplates。如何才能做到这一点?
2 回答
How can this be done?
It can't be done. You will have to write a custom ValidationMessageFor helper. The generated HTML is not extensible and is hardcoded in the built-in helper. You may download the ASP.NET MVC 3 source code and see how the default helper is implemented. This could be used as a starting point for writing your custom helper that will generate the desired markup.
The view can use the custom HtmlHelper method by adding a reference to the method's namespace.
My view, after using @Darin Dimitrov's guidance, looks similar to the following code:
@model Project.Models.MyModel
@using Project.Helper;
...
@Html.ValidationMessageFor_ThatYouMade(model => model.PropertyBeingValidated)