173901
173901

Reputation: 709

asp MVC adding a title inside a TextBoxFor()

I am making a registration form for my website, and currently i have it like this enter image description here and the code for that is :

@Html.Label("Firstname")
@Html.TextBoxFor(model => model.Firstname, new {@required = "require", @style = "width:75%;", @class = "form-control" })
@Html.ValidationMessageFor(model => model.Firstname, null, new { @style = "color:red;" })
<br />
@Html.Label("Surname")
@Html.TextBoxFor(model => model.Lastname, new { @required = "require", @style = "width:75%;", @class = "form-control" })
@Html.ValidationMessageFor(model => model.Lastname, null, new { @style = "color:red;" })
<br /> . . . 

What i am trying to do is to add the title for example "First Name" inside the textbox, when data is entered it would disappear, To look like this enter image description here

I have tried adding:

@name ="FirstName"
@label ="FirstName"
@title ="FirstName"

but all of those did nothing.

Upvotes: 0

Views: 3025

Answers (3)

Fals
Fals

Reputation: 6839

You are talking about placeholder. It's a HTML attribute, just add it to the custom htmlAttributes parameters, for exemple:

@Html.TextBoxFor(model => model.Firstname, new {@required = "require", @style = "width:75%;", @class = "form-control", placeholder = "First Name" })

Upvotes: 3

Daniel J.G.
Daniel J.G.

Reputation: 34992

It seems you are looking for the Html5 placeholder attribute

@Html.TextBoxFor(model => model.Firstname, new {@required = "require", @style = "width:75%;", @class = "form-control", placeholder = "First Name" })

Upvotes: 1

D&#225;vid Kaya
D&#225;vid Kaya

Reputation: 934

This will add default value to your TextBox

@Html.TextBoxFor(model => model.Firstname, new {@required = "require", 
                                                @style = "width:75%;", 
                                                @class = "form-control", 
                                                @value = "FirstName"})

Or if you want it to dissapear on entering the textbox

@Html.TextBoxFor(model => model.Firstname, new {@required = "require", 
                                                @style = "width:75%;", 
                                                @class = "form-control", 
                                                placeholder = "FirstName"})

Upvotes: 0

Related Questions