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".
If you want to set a hint for text area or input field, then use the HTML placeholder attribute. The hint is the expected value, which gets displayed before the user enters a value, for example, name, details, etc.
you have to use TextBoxFor instead of it try this code: @Html. TextBoxFor(model => model. ToEmail, new {placeholder="Enter Your EmailID...!!!"})
IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.
The placeholder attribute specifies a short hint that describes the expected value of an input field (e.g. a sample value or a short description of the expected format). The short hint is displayed in the input field before the user enters a value.
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" } )
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With