4

I used [prompt("name")] in Controller and

@Html.EditorFor(model => model.Email,
new {placeholder=ViewData.ModelMetadata.Watermark})`

in view but nothing showed I also used help of Html5 Placeholders with .NET MVC 3 Razor EditorFor extension? but nothing happened I need Placeholder for @EditorFor not for Textbox Any Help Appreciated

Community
  • 1
  • 1
Suyash Kumar Singh
  • 173
  • 1
  • 2
  • 10

3 Answers3

5

You can write HTML attributes into Html.EditorFor using the following syntax. Make sure you use @ before class and placeholder so that the renderer knows this is HTML markup.

@Html.EditorFor(model => model.Email, new { 
    htmlAttributes = new { 
        @class = "form-control", 
        @placeholder = "Email Address"
    }
})
Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
user7900201
  • 51
  • 1
  • 2
  • Typically only reserved keywords like `class` require the `@` prefix in an `htmlAttributes` object, "placeholder" is fine without it. Not always consistent though; strangely, setting a "value" isn't parsed as a reserved word but requires using proper case (`@Value`) to be applied correctly. – brichins Mar 28 '18 at 17:55
2

EditorFor doesn't accept HtmlAttribut argument you have to use TextBoxFor instead of it ;) try this:

@Html.TextBoxFor(model => model.Email, new  {placeholder=ViewData.ModelMetadata.Watermark})`
  • Thanks But do you know why this is so for `@EditoreFor ` ? @TRIKI Khaled – Suyash Kumar Singh Apr 02 '15 at 11:36
  • the Html.EditorFor works with different types and would generate an Html input elements with different types according to the type of the attribut you've entred as argument. you can check the classes and see check differences between both of them : EditorExtensions and InputExtensions – TRIKI Khaled Apr 02 '15 at 12:52
  • As of MVC 5.1, EditorFor can handle HtmlAttributes. This was one of the added features. – Erik Funkenbusch Apr 02 '15 at 22:48
0

Work for me just with TextBoxFor.

   @Html.TextBoxFor(model => model.emailContact, new {@placeholder = "Your Placeholder Text" }
rickslayer
  • 29
  • 1