Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to use placeholder in @Html.EditorFor for text in mvc. I tried many of things even search in google but whatever the answer I got that not work. I surprised...!!!. And finally I ask in question.

My lastest code for placeholder is

@Html.EditorFor(model => model.ToEmail, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Your EmailID...!!!" } })


This code also not work.

Thank You.

What I have tried:

I tried many things but not work.
Posted
Updated 12-Jul-19 3:05am

You've tagged your question as MVC4. Support for custom HTML attributes in EditorFor was added in MVC 5.1:
We now allow passing in HTML attributes in EditorFor as an anonymous object.

You'll need to upgrade your project to MVC 5.1:
How to Upgrade an ASP.NET MVC 4 and Web API Project to ASP.NET MVC 5 and Web API 2 | Microsoft Docs[^]
 
Share this answer
 
EditorFor doesn't accept HtmlAttribut! you have to use TextBoxFor instead of it try this code:

@Html.TextBoxFor(model => model.ToEmail, new {placeholder="Enter Your EmailID...!!!"})
 
Share this answer
 
Comments
CHill60 12-Jul-19 10:21am    
Did you read the documentation on the link provided by Richard Deeming "We now allow passing in HTML attributes in EditorFor as an anonymous object." It even gives an example
@Html.EditorFor(model => model, new { htmlAttributes = new { @class = "form-control" }, })

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900