Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use placeholder attribute with Html.EditorFor?

I want to use the placeholder attribute in the Html.EditorFor so I did just like in the first answer: Html5 Placeholders with .NET MVC 3 Razor EditorFor extension?

But in the final part, I don't have the EditorTemplates folder so I created it and when I tried to create the string.cshtml view I couldn't because the name "string" is reserved so I chose stringg.cshtml instead and it didn't work! Do I have to change the name elsewhere in my program? I did exactly like the answer...

Thank you!

like image 661
caj Avatar asked May 04 '14 10:05

caj


People also ask

How do you put a placeholder in HTML?

Definition and UsageThe short hint is displayed in the input field before the user enters a value. Note: The placeholder attribute works with the following input types: text, search, url, tel, email, and password.

Is placeholder a HTML attribute?

Definition and UsageThe placeholder attribute specifies a short hint that describes the expected value of a input field / textarea. The short hint is displayed in the field before the user enters a value.

How can add placeholder in textbox in MVC?

you have to use TextBoxFor instead of it try this code: @Html. TextBoxFor(model => model. ToEmail, new {placeholder="Enter Your EmailID...!!!"})

How does HTML EditorFor work?

Simply put, the Html. EditorFor method allows the developer to retain control over the display of form elements by data type (ie. string, boolean, int…etc) or model attribute at a global level rather than at an individual view level. This allows for cleaner ASP markup and easily scalable form controls.


2 Answers

Upgrade to MVC 5.1 and you can use HTML attributes in EditorFor:

@Html.EditorFor(m => m.variable, new { htmlAttributes = new { placeholder = "Your Placeholder Text" } }) 

http://www.asp.net/mvc/overview/releases/mvc51-release-notes

like image 125
Medo Avatar answered Oct 02 '22 23:10

Medo


 @Html.EditorFor(model => model.members_ssn, new { htmlAttributes = new { @class = "form-control", placeholder = "Your Example Here" } }) 
like image 32
Jay Avatar answered Oct 02 '22 21:10

Jay