MVC4 input field placeholder
Solution 1:
An alternative to using a plugin is using an editor template. What you need to do is to create a template file in Shared\EditorTemplates
folder and call it String.cshtml
. Then put this in that file:
@Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue,
new { placeholder = ViewData.ModelMetadata.Watermark })
Then use it in your view like this:
@Html.EditorFor(m=>Model.UnitPercent)
The downside, this works for properties of type string
, and you will have to create a template for each type
that you want support for a watermark.
Solution 2:
I did so
Field in model:
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
Razor:
<li>
@Html.TextBoxFor(m => m.UserName, new { placeholder = Html.DisplayNameFor(n => n.UserName)})
</li>
Solution 3:
Of course it does:
@Html.TextBoxFor(x => x.Email, new { @placeholder = "Email" })
Solution 4:
You can easily add Css class, placeholder , etc. as shown below:
@Html.TextBoxFor(m => m.Name, new { @class = "form-control", placeholder="Name" })
Hope this helps