Reputation: 8341
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?
Upvotes: 21
Views: 55142
Reputation: 2797
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.
Upvotes: 2
Reputation: 1129
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)]
Upvotes: -1
Reputation: 229
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!
Upvotes: 10
Reputation: 394
Use TextBoxFor:
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.Email) })
Upvotes: 5
Reputation: 56466
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.
Upvotes: 7
Reputation: 389
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" , @placeholder = "Username" })
Upvotes: 2
Reputation: 639
Use TextBoxFor like so:
@Html.TextBoxFor(model => model.LastName, new{placeholder="Last Name"})
Upvotes: 1
Reputation: 8341
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)
Upvotes: 44
Reputation: 4187
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.
Upvotes: -1
Reputation: 5580
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 })
Upvotes: 0