asp.net mvc - Html5 Placeholders with .NET MVC 3 Razor EditorFor extension? -


is there way write html5 placeholder using @html.editorfor, or should use textboxfor extension i.e.

@html.textboxfor(model => model.title, new { @placeholder = "enter title here"}) 

or make sense write our own custom extension can maybe use 'description' display attribute via dataannotations (similar this)?

of course, same question applies 'autofocus' well.

you may take @ following article writing custom dataannotationsmodelmetadataprovider.

and here's another, more asp.net mvc 3ish way proceed involving newly introduced imetadataaware interface.

start creating custom attribute implementing interface:

public class placeholderattribute : attribute, imetadataaware {     private readonly string _placeholder;     public placeholderattribute(string placeholder)     {         _placeholder = placeholder;     }      public void onmetadatacreated(modelmetadata metadata)     {         metadata.additionalvalues["placeholder"] = _placeholder;     } } 

and decorate model it:

public class myviewmodel {     [placeholder("enter title here")]     public string title { get; set; } } 

next define controller:

public class homecontroller : controller {     public actionresult index()     {         return view(new myviewmodel());     } } 

a corresponding view:

@model myviewmodel @using (html.beginform()) {     @html.editorfor(x => x.title)     <input type="submit" value="ok" /> } 

and editor template (~/views/shared/editortemplates/string.cshtml):

@{     var placeholder = string.empty;     if (viewdata.modelmetadata.additionalvalues.containskey("placeholder"))     {         placeholder = viewdata.modelmetadata.additionalvalues["placeholder"] string;     } } <span>     @html.label(viewdata.modelmetadata.propertyname)     @html.textbox("", viewdata.templateinfo.formattedmodelvalue, new { placeholder = placeholder }) </span> 

Comments