I have an MVC 2 application that utilises forms. The required fields within the form are set using attributes that update the model metadata. The form fields are created using the MVC HtmlHelper method : Html.EditorFor.
This works fine so far as validation is concerned, but it seems that by default the required fields are not displayed to the user (for example by appending a * to the control).
If I wanted to provide some custom means of displaying this fact to the user (lets immagine I want to change the background colour of the edit control for example), where is the best place to do it? Would I need to create a custom html helper to replace EditorFor? I tried but it seemed difficult to gain access to the metadata for the correct property. I already have a custom ModelMetaDataProvider so there is no problem adding it there if that is the correct place.
Rather than a custom helper, you probably want a custom template. Your custom template would mimic the standard template, except that you’d have the marker for required fields. See Brad Wilson’s blog article on templating in MVC2 for more info on how to go about it, but the basic idea is to generate a ViewUserControl in the Shared/EditorTemplates (and Shared/DisplayTemplates) folders strongly typed to your model. You can then use EditorFor and DisplayFor and specify the template name to use, though I think it will also pick up the template if it’s strongly typed to your model.