如何在C#/MVC 4的Html.TextBoxFor中输入占位符文本 [英] How to Enter Placeholder Text Within Html.TextBoxFor in C# / MVC 4

查看:257
本文介绍了如何在C#/MVC 4的Html.TextBoxFor中输入占位符文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Typically in HTML / CSS, if you want to add placeholder text to a textbox you would simply do this:

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

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

But since I'm using the existing code that's provided for a login panel in Visual Studio MVC 4:

/Views/Account/Login.cshtml

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

This is the C# code that's currently rendering the inputs:

@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" })

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

How do you add placeholder text to this code in C#? I tried this:

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

它在红色的占位符"下面加了下划线,表示在当前上下文中不存在占位符"名称".

And it underlined 'placeholder' in red saying "The name 'placeholder' does not exist in the current context".

推荐答案

使用带有htmlAttributes参数的TextBoxFor()重载.此参数应该是一个匿名对象,它具有要分配给输入的 all 属性.

Use an overload of TextBoxFor() with an htmlAttributes argument. This argument should be an anonymous object with all attributes you wish to assign to the input.

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

For example, if you want to set the placeholder and class attributes:

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

这篇关于如何在C#/MVC 4的Html.TextBoxFor中输入占位符文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆