21

I have a model:

[DataType(DataType.EmailAddress)]
[DisplayFormat(ConvertEmptyStringToNull = true)]
[Display(Prompt = "Email Address")]
public string Email { get; set; }

I am trying to get the "prompt" to show in the placeholder text of the resulting text box with the following:

@Html.EditorFor(model => model.Email, 
new { htmlAttributes = new { @class = "form-control input-md",
placeholder = @ViewData.ModelMetadata.Watermark } })

When I view the generated HTML I only get "placeholder" in the input tag. According to what I have read ViewData.ModelMetadata.Watermark should work. What is the correct way to get this placeholder text in place?

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
John S
  • 7,909
  • 21
  • 77
  • 145
  • This sort of thing can be done with `[UIHint]` and a custom template - see [this answer](https://stackoverflow.com/a/14571765/957950) to a similar question. – brichins Mar 28 '18 at 18:13

10 Answers10

43

This solved my issue:

@Html.EditorFor(model => model.Email, new { htmlAttributes = 
new { @class = "form-control input-sm", 
placeholder = @Html.DisplayNameFor(m=>m.Email) } })

The code that did it was

placeholder = @Html.DisplayNameFor(m=>m.Email) 
John S
  • 7,909
  • 21
  • 77
  • 145
  • 1
    But when i use DisplyName and Display attribute, one is for label and another is for Placeholder then this is showing me same for both in Name and Placeholder which i define in DisplayName – Smit Patel Jun 02 '16 at 11:44
  • 1
    This will double-escape the display name - no problem for "Email", but if you're displaying for example text with accents (french), this will be a problem. – marapet Apr 25 '17 at 13:45
10

A little late, but if someone is still looking for it...

@Html.EditorFor(model => model.Email, 
new { htmlAttributes = new { @class = "form-control input-md",
@placeholder = "Whatever you want as a placeholder" } })

It's perfect and clean!

7

The correct solution to get the Prompt value instead of the DisplayName in a non-templated control context is the following:

@Html.EditorFor(model => model.Email, 
    new { @class = "form-control input-md",
    placeholder = ModelMetadata.FromLambdaExpression(m => m.Email, ViewData).Watermark
})

This will also - unlike the accepted solution using @Html.DisplayNameFor(m=>m.Email) - not double-escape the watermark text, which depending on the text and the language displayed can be a problem.

marapet
  • 54,856
  • 12
  • 170
  • 184
5

Use TextBoxFor:

@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.Email) })
Shalom Dahan
  • 335
  • 7
  • 19
2
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" , @placeholder = "Username" })
Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
Muhammad Bilal
  • 359
  • 3
  • 5
2

I know, i'm a bit late, but an extension method ad-hoc like this one is the best solution imho:

public static string UnencodedDisplayNameFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    var displayName = html.DisplayNameFor(expression).ToString();
    var unencodedDisplayName = HttpUtility.HtmlDecode(displayName);

    return unencodedDisplayName;
}

Usage:

@Html.EditorFor(model => model.Email, new
{
    htmlAttributes = new
    {
        @class = "form-control input-sm",
        placeholder = Html.UnencodedDisplayNameFor(m => m.Email) 
    }
})

This solution won't add complexity (like using ModelMetadata) or duplicate labels (like some other answers), and provides full support on accents.

T-moty
  • 2,679
  • 1
  • 26
  • 31
1

Use TextBoxFor like so:

 @Html.TextBoxFor(model => model.LastName, new{placeholder="Last Name"})
dc922
  • 629
  • 3
  • 14
  • 27
0

In your controller method that renders view say

ViewData["Name"] = "blabbity blah";

then

@Html.TextBoxFor(u => u.Company, new { @placeholder = @ViewData["Name"] })

Actually better yet you can do this.

 public ActionResult Index()
    {              
          NewLogin n = new ClassModel().PopModel();
          n.Company = "fsdfsdf";

          return View(n);
    }

@Html.TextBoxFor(u => u.Company, new { @placeholder = Model.Company })

CSharper
  • 5,420
  • 6
  • 28
  • 54
  • 1
    I really want to avoid TextBoxFor and stick with EditorFor to keep the data annotations and validations in place. – John S Feb 28 '14 at 22:00
-1

Check out this answers to this question, which has been answered aleady (a few times).

@Html.EditorFor uses templates, and so the approach to this problem is using a customised template.

Community
  • 1
  • 1
Prabu
  • 4,097
  • 5
  • 45
  • 66
  • The linked questions don't address the specific question of populating "placeholder" from an attribute tag. However, using a customized template is a valid approach here - would be nice to have example code here though. – brichins Mar 28 '18 at 18:08
-1

you can use it as follows for the date which will also give you a "datetime" picker.

[DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}",ApplyFormatInEditMode =true)]
FortyTwo
  • 2,414
  • 3
  • 22
  • 33
Sabin Kumar Sharma
  • 1,081
  • 2
  • 8
  • 12