2014-05-12 40 views
0

我想提出一個登記表,我的網站,目前我有像這樣 enter image description here 和代碼是內部的一個標題:ASP MVC添加TextBoxFor()

@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 /> . . . 

我是什麼試圖做的是添加標題,例如「名」文本框裏面,輸入數據時,它會消失,看起來像這樣 enter image description here

我曾嘗試加入:

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

但所有這些沒有做任何事。

+0

你試過'@value =「姓」'? –

回答

3

您正在談論placeholder。這是一個HTML屬性,只需將其添加到自定義htmlAttributes參數,爲例:

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

非常感謝,這工作完美 – 173901

0

這將默認值添加到您的TextBox

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

或者,如果你希望它在進入文本框

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

好像你正在尋找HTML5的placeholder attribute

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