How to Enter Placeholder Text Within Html.TextBoxFor in C# / MVC 4

Viewed 74043

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" })

enter image description here

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".

6 Answers

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

This works for me...

@Html.TextBoxFor(m => m.Username, new { @placeholder = "Username", @class = "input100" })

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" })
Related