Ian Davis
Ian Davis

Reputation: 19423

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!

Upvotes: 10

Views: 13902

Answers (3)

Marcel Popescu
Marcel Popescu

Reputation: 3341

I realize this has been asked a long, long time ago but the Display attribute with the Prompt argument is probably what you want here.

    [Display(Prompt = "Enter the country (default US)")]
    public string Country { get; set; }

Upvotes: 4

PhilW
PhilW

Reputation: 410

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.

Upvotes: 11

Zach Green
Zach Green

Reputation: 3460

I don't think so, but you could write your own custom helper and attribute to do it. http://www.aspnetwiki.com/page:creating-custom-html-helpers

Upvotes: 0

Related Questions