Reputation: 4101
Typically in HTML / CSS, if you want to add placeholder text to a textbox you would simply do this:
<input type="text" class="input-class" placeholder="Please enter your email"/>
But since I'm using the existing code that's provided for a login panel in Visual Studio MVC 4:
/Views/Account/Login.cshtml
This is the C# code that's currently rendering the inputs:
@Html.TextBoxFor(m => m.Email, new { @class = "form-input" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
@Html.PasswordFor(m => m.Password, new { @class = "form-input" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
How do you add placeholder text to this code in C#? I tried this:
@Html.TextBoxFor(m => m.Email, placeholder ="Email" new { @class = "form-input" })
And it underlined 'placeholder' in red saying "The name 'placeholder' does not exist in the current context".
Upvotes: 30
Views: 86592
Reputation: 1097
This works for me...
@Html.TextBoxFor(m => m.Username, new { @placeholder = "Username", @class = "input100" })
Upvotes: 6
Reputation: 1311
For input field
@Html.TextBoxFor( m => m.Email, new { placeholder = "Your email id" })
For textarea
@Html.TextAreaFor(m => m.Description, new { placeholder = "Please add description here" })
Upvotes: 4
Reputation: 272
Try to the following
This code is tested and it's working
@Html.TextBox("CustomarName" ,null, new { @class = "form-control" , @placeholder = "Search With Customar Name" })
Hope it helps to you
Upvotes: 11
Reputation: 1631
There is a parameter which is objecthtmlattributes , You can set every html input attribute there
Example:
@Html.TextBox("Model binding here" , new { @class="form-controll" , @placeholder="Enter email"})
Upvotes: 2
Reputation: 54377
Use an overload of TextBoxFor()
with an htmlAttributes
argument. This argument should be an anonymous object with all attributes you wish to assign to the input.
For example, if you want to set the placeholder
and class
attributes:
@Html.TextBoxFor( m => m.Email, new { placeholder = "Email", @class = "form-input" } )
Upvotes: 72
Reputation: 1298
Try this:
@Html.TextBoxFor(m => m.Email, new { placeholder = "Email" })
Upvotes: 4