HappyHands31
HappyHands31

Reputation: 4101

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

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

Upvotes: 30

Views: 86592

Answers (6)

Yusuf Ganiyu
Yusuf Ganiyu

Reputation: 1097

This works for me...

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

Upvotes: 6

Vinod Kumar
Vinod Kumar

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

Mizanur Rahman
Mizanur Rahman

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

Saurabh
Saurabh

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

Tim M.
Tim M.

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

HelmBurger
HelmBurger

Reputation: 1298

Try this:

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

Upvotes: 4

Related Questions