asp.net-mvc MVC4 输入字段占位符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16031385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC4 input field placeholder
提问by Tomas
Does MVC4by default support placeholdersfor generated input fields? I didn't found anything so I am trying to implement my own but unfortunately Prompt = "E-Mail"is not passed to ViewData.ModelMetadata.Watermarkwhile generating control. Why?
MVC4默认情况下是否支持placeholders生成的输入字段?我没有找到任何东西,所以我试图实现我自己的但不幸的Prompt = "E-Mail"是ViewData.ModelMetadata.Watermark在生成控制时没有传递给。为什么?
Model
模型
public class LogOnModel
{
[Required]
[Display(Name = "E-Mail", Prompt = "E-Mail")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
}
@Html.TextBoxFor(m => m.Email, new { placeholder = ViewData.ModelMetadata.Watermark })
I get html code where placeholdertag do not has any text
我得到 html 代码,其中placeholder标签没有任何文本
<input data-val="true" data-val-regex="Please enter a valid e-mail address" data-val-required="The E-Mail field is required." id="Email" name="Email" placeholder="" type="text" value="" class="valid">
采纳答案by von v.
An alternative to using a plugin is using an editor template. What you need to do is to create a template file in Shared\EditorTemplatesfolder and call it String.cshtml. Then put this in that file:
使用插件的替代方法是使用编辑器模板。您需要做的是在Shared\EditorTemplates文件夹中创建一个模板文件并调用它String.cshtml。然后把它放在那个文件中:
@Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue,
new { placeholder = ViewData.ModelMetadata.Watermark })
Then use it in your view like this:
然后在您的视图中使用它,如下所示:
@Html.EditorFor(m=>Model.UnitPercent)
The downside, this works for properties of type string, and you will have to create a template for each typethat you want support for a watermark.
不利的一面是,这适用于 type 的属性string,您必须为每个type要支持水印的模板创建一个模板。
回答by vlad
I did so
我这样做了
Field in model:
模型中的字段:
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
Razor:
剃刀:
<li>
@Html.TextBoxFor(m => m.UserName, new { placeholder = Html.DisplayNameFor(n => n.UserName)})
</li>
回答by Panos
Of course it does:
当然可以:
@Html.TextBoxFor(x => x.Email, new { @placeholder = "Email" })
回答by reaz
You can easily add Css class, placeholder , etc. as shown below:
您可以轻松添加 Css 类、占位符等,如下所示:
@Html.TextBoxFor(m => m.Name, new { @class = "form-control", placeholder="Name" })
Hope this helps
希望这可以帮助
回答by Morten Holmgaard
This works:
这有效:
@Html.TextBox("name", null, new { placeholder = "Text" })
回答by Smit Patel
There are such of ways to Bind a Placeholder to View:
将占位符绑定到视图的方法有以下几种:
1) With use of MVC Data Annotations:
1)使用MVC数据注释:
Model:
模型:
[Required]
[Display(Prompt = "Enter Your First Name")]
public string FirstName { get; set; }
Razor Syntax:
剃刀语法:
@Html.TextBoxFor(m => m.FirstName, new { placeholder = @Html.DisplayNameFor(n => n.UserName)})
2) With use of MVC Data Annotations But with DisplayName:
2) 使用 MVC 数据注释但使用 DisplayName:
Model:
模型:
[Required]
[DisplayName("Enter Your First Name")]
public string FirstName { get; set; }
Razor Syntax:
剃刀语法:
@Html.TextBoxFor(m => m.FirstName, new { placeholder = @Html.DisplayNameFor(n => n.UserName)})
3) Without use of MVC Data Annotation (recommended):
3)不使用MVC数据注释(推荐):
Razor Syntax:
剃刀语法:
@Html.TextBoxFor(m => m.FirstName, new { @placeholder = "Enter Your First Name")
回答by Erik Schierboom
By default, it does not. However, you can use the MVCHtml5ToolkitNuGet package that has HTML helpers that can output HTML5. For your example, after installing the toolkit you can then use the following HTML helper call:
默认情况下,它没有。但是,您可以使用具有可输出 HTML5 的 HTML 帮助程序的MVCHtml5ToolkitNuGet 包。对于您的示例,安装工具包后,您可以使用以下 HTML 帮助程序调用:
@Html.Html5TextBoxFor(m => m.Email, InputTypes.InputType.Email)
This will output the following HTML:
这将输出以下 HTML:
<input id="Email" name="Email" placeholder="E-Mail" type="Email" value="">
As can be seen, the placeholder is now correctly rendered.
可以看出,占位符现在已正确呈现。
回答by marapet
The correct solution to get the Promptvalue in a non-templated control context is:
Prompt在非模板化控件上下文中获取值的正确解决方案是:
@Html.TextBoxFor(model => model.Email,
new { placeholder = ModelMetadata.FromLambdaExpression(m => m.Email, ViewData).Watermark }
)
This will also notdouble-escape the watermark text.
这也不会双重转义水印文本。
回答by ankit dixit
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control",@placeholder = "Name" })
回答by Gilli Suresh
Try this:
尝试这个:
@Html.TextbBoxFor(x=>x.Email,new { @[email protected]}
If this possible or else what could be the way
如果这可能,否则可能是什么方式

