asp MVC在TextBoxFor()中添加标题

时间:2014-05-12 20:02:00

标签: c# asp.net asp.net-mvc

我正在为我的网站制作一个注册表,目前我就是这样的 enter image description here 并且代码是:

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

我想要做的是添加标题例如&#34;名字&#34;在文本框内,当输入数据时,它会消失,看起来像这样 enter image description here

我尝试过添加:

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

但所有这些都没有做任何事情。

3 个答案:

答案 0 :(得分:3)

您在谈论placeholder。它是一个HTML属性,只需将其添加到自定义htmlAttributes参数中即可:

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

答案 1 :(得分:1)

您似乎在寻找Html5 placeholder attribute

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

答案 2 :(得分: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"})