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

Hap*_*s31 15 html css c# 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" })
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

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

并且它用红色标出"占位符",称"当前上下文中不存在名称'占位符'".

Tim*_*ora 35

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

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

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


小智 6

这对我有用...

@Html.TextBoxFor(m => m.Username, new { @placeholder = "Username", @class = "input100" })
Run Code Online (Sandbox Code Playgroud)


小智 5

尝试以下

此代码已经过测试,可以正常工作

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

希望对您有帮助

  • OP 询问如何在“TextBoxFor”中而不是“TextBox”中使用占位符 (2认同)