ASP.NET MVC TextBox For Placeholder

I have the following text box in the View :

enter image description here

Code for placeholder:

 @Html.TextBoxFor(m => m.Listing.Location.AddressLine1, new {@placeholder="Address Line 1 - Required"}) 

Question: How to make the text "Required" in RED.

+7
source share
2 answers

You cannot do this with a text box. You have no control over the formatting of only certain parts of the HTML5 placeholder attribute. Not to mention that if you want to get multi-color text, you can no longer use a text box or text box. You will need to use an element with the attribute contenteditable="true" and write your own plugin.

Here, for example, what the markup looks like:

 <div contenteditable="true"> Address Line 1 - <span style="color: red">Required</span>; </div> 

But since this is a div element, you will need to create a backup with the corresponding hidden field so that the value is sent to the server. Also, when the value of this field changes, you also need to re-synchronize the value of the hidden field. It will be a lot of work.

+4
source

in the model

[Required]

 public string AddressLine1{ get; set; } @Html.TextBoxFor(model => model.Listing.Location.AddressLine1, new { @class = "form-control" , placeholder = "Address Line 1 - Required" }) 

try this for empty @ Html.TextBox

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

All Articles