Reputation: 10100
I'm using ASP.NET MVC 3 RTM. Is it possible to change the HTML rendered from a View Model, by using an attribute?
Example:
public class Product
{
[AddHtmlAttribute(Name = "disabled", Value = "disabled")]
public string Name { get; set; }
}
I want the attribute to be able to change the rendered HTML, that property results in. I know it properly can't be done with an attribute alone. I probably have to hook into the system by implementing an interface, but where should I look?
I know MVC uses the default editor templates, and I've looked at them in the MVC 3 source code, but I haven't been able to figure out if it would be possible to somehow hook into the rendered element and add some attributes. I know the validation system does this, by adding custom HTML attributes to support unobtrusive validation.
I guess I just need a pointer to where I should look, or what interface I should take a look at.
Thank you so much.
Update: I'm using the standard HTML helper Html.EditorFor(model => model.Name)
for my fields, and haven't overriden any editor templates. I would really prefer if I didn't have to change or override the default templates.
Upvotes: 4
Views: 2549
Reputation: 1038890
You may checkout the following blog post which illustrates how to achieve this by writing a custom DataAnnotationsModelMetadataProvider
, attribute and overriding the default templates.
Upvotes: 5