Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 877
HappyHands31 Avatar asked May 17 '17 00:05

HappyHands31


People also ask

How do you put a placeholder in HTML?

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.

How can use placeholder in MVC textbox?

you have to use TextBoxFor instead of it try this code: @Html. TextBoxFor(model => model. ToEmail, new {placeholder="Enter Your EmailID...!!!"})

What is the difference between HTML textbox vs HTML TextBoxFor?

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.

How do you use placeholder input?

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.


2 Answers

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" } ) 
like image 115
Tim M. Avatar answered Sep 22 '22 22:09

Tim M.


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

like image 36
Mizanur Rahman Avatar answered Sep 19 '22 22:09

Mizanur Rahman