如何在C#/ MVC 4中的Html.TextBoxFor中输入占位符文本

时间:2017-05-17 00:33:28

标签: c# html css asp.net asp.net-mvc-4

通常在HTML / CSS中,如果要将占位符文本添加到文本框,则只需执行此操作:

<input type="text" class="input-class" placeholder="Please enter your email"/>

但是因为我正在使用为Visual Studio MVC 4中的登录面板提供的现有代码:

/Views/Account/Login.cshtml

这是当前呈现输入的C#代码:

@Html.TextBoxFor(m => m.Email, new { @class = "form-input" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })

@Html.PasswordFor(m => m.Password, new { @class = "form-input" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })

enter image description here

如何在C#中为此代码添加占位符文本?我试过这个:

@Html.TextBoxFor(m => m.Email, placeholder ="Email" new { @class = "form-input" })

它强调“占位符”用红色表示“名称'占位符'在当前上下文中不存在”。

6 个答案:

答案 0 :(得分:27)

使用带有TextBoxFor()参数的htmlAttributes重载。此参数应该是一个匿名对象,其中包含要分配给输入的所有属性。

例如,如果您要设置placeholderclass属性:

@Html.TextBoxFor( m => m.Email, new { placeholder = "Email", @class = "form-input" } )

答案 1 :(得分:3)

这对我有用...

@Html.TextBoxFor(m => m.Username, new { @placeholder = "Username", @class = "input100" })

答案 2 :(得分:2)

尝试以下

  

此代码经过测试且正常运行

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

希望对你有所帮助

答案 3 :(得分:1)

试试这个:

@Html.TextBoxFor(m => m.Email, new { placeholder = "Email" })

答案 4 :(得分:1)

有一个参数是objecthtmlattributes,你可以在那里设置每个html输入属性
例如:

 @Html.TextBox("Model binding here" , new { @class="form-controll" , @placeholder="Enter email"})

答案 5 :(得分:1)

用于输入字段

@Html.TextBoxFor( m => m.Email, new { placeholder = "Your email id" })

用于文本区域

@Html.TextAreaFor(m => m.Description, new { placeholder = "Please add description here" })