Search code examples
c#asp.net-mvchtmlviewmodel

Provide data annotation for placeholder attr for textbox in MVC


Is there a way to have a data annotation for what should be in placeholder attr on a textbox in an MVC view?

Example:

In my ViewModel.cs, something like:

[Placeholder="First name"]
public string FirstName { get; set; }

In my view:

@this.Html.TextBoxFor(m => m.FirstName)

If that could render this:

<input type="text" placeholder="First name" ... />

Is this possible? Thanks!


Solution

  • try

    @this.Html.TextBoxFor(m => m.FirstName, new{placeholder="First name"})
    

    Ah, not in the model.

    You can define your own custome attribute http://blogs.msdn.com/b/aspnetue/archive/2010/02/24/attributes-and-asp-net-mvc.aspx. I think you will need a custom html helper to generate the html.