Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 724
Ian Davis Avatar asked Jan 31 '12 19:01

Ian Davis


People also ask

Which name space are required to data annotation using MVC?

ComponentModel. DataAnnotations namespace that you can use to apply to (decorate) classes or properties to enforce pre-defined validation rules.

What is data annotation in MVC C#?

DataAnnotations is used to configure your model classes, which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations.


1 Answers

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.

like image 101
PhilW Avatar answered Oct 24 '22 11:10

PhilW