With MVC, when you use something like @Html.TextBoxFor(…) the control renders using property attributes from the model for things like validation.
It also uses the class’s property name as the name of the rendered HTML control. This conflicts a little with our naming conventions so I’d like to be able to control the ID a bit more.
I’ve added the ‘htmlAttributes:’ to the helper, which does what I want, but I was wondering:
- How does this impact MVC’s ability to instantiate the model again
when the information is posted? - Is there a way to specify the control ID using property attributes in the model class?
Thanks,
Jacques
The ID attribute has absolutely no impact on anything that is posted. The ID attribute is never used when submitting a form. The only requirement is that this attribute is unique throughout your entire DOM. It is the
nameattribute that is used by the model binder. You cannot change this attribute anyway using the htmlAttributes because this attribute is inferred from the lambda expression used as first argument and you shouldn’t need to change it anyway, otherwise you could break the way the default model binder rehydrates your view models.Yes, but it could be a little more work. You could use custom editor templates for the standard types and a custom metadata aware attribute that will pass this additional metadata information to the custom editor template. If you are interested in the implementation specifics I could provide an example but first I have to understand why you need that.