Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC TextBoxFor Placeholder

Tags:

asp.net-mvc

I have the following textbox in my View:

enter image description here

The code for the placeholder:

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

Question: How do I make the text "Required" in RED.

like image 420
Subby Avatar asked Oct 15 '12 15:10

Subby


People also ask

How can use placeholder in ASP NET MVC?

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

What is textbox and TextBoxFor in MVC?

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.


1 Answers

You can't do this with a text field. You have no control over the formatting of only some portions of the HTML5 placeholder attribute. Not to mention that if you want to achieve multicolor text you can no longer use a text field or textarea. You will have to use an element with contenteditable="true" attribute and write your custom plugin.

Here's for example how the markup might look like:

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

But since this is a div element you will now have to back it up with a corresponding hidden field so that the value is sent to the server. Also when the value of this field changes you need to resynchronize the hidden field value as well. It will be a lot of work.

like image 184
Darin Dimitrov Avatar answered Oct 01 '22 02:10

Darin Dimitrov