How to enter placeholder text inside Html.TextBoxFor in C # / MVC 4

Usually in HTML / CSS, if you want to add placeholder text in a text box, you simply do the following:

<input type="text" class="input-class" placeholder="Please enter your email"/>

But since I am using existing code that provided the login panel in Visual Studio MVC 4:

/Views/Account/Login.cshtml

This is C # code that currently passes 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" }) 

enter image description here

How do you add placeholder text to this C # code? I tried this:

 @Html.TextBoxFor(m => m.Email, placeholder ="Email" new { @class = "form-input" }) 

And he emphasized "placeholder" in the red expression "Placeholder-name" does not exist in the current context. "

+8
source share
5 answers

Use the TextBoxFor() overload with the htmlAttributes argument. This argument must be an anonymous object with all the attributes that you want to assign for input.

For example, if you want to set the placeholder and class attributes:

 @Html.TextBoxFor( m => m.Email, new { placeholder = "Email", @class = "form-input" } ) 
+23
source

Try the following

This code has been verified and works.

 @Html.TextBox("CustomarName" ,null, new { @class = "form-control" , @placeholder = "Search With Customar Name" }) 

Hope this helps you.

+2
source

Try the following:

 @Html.TextBoxFor(m => m.Email, new { placeholder = "Email" }) 
+1
source

There is a parameter that is objecthtmlattributes, you can set each html input attribute
Example:

  @Html.TextBox("Model binding here" , new { @class="form-controll" , @placeholder="Enter email"}) 
+1
source

For input field

 @Html.TextBoxFor( m => m.Email, new { placeholder = "Your email id" }) 

For text area

 @Html.TextAreaFor(m => m.Description, new { placeholder = "Please add description here" }) 
0
source

All Articles