MVC4 输入字段占位符 [英] MVC4 input field placeholder

查看:22
本文介绍了MVC4 输入字段占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC4 是否默认支持 placeholders 生成的输入字段?我没有找到任何东西,所以我试图实现我自己的但不幸的是 Prompt = "E-Mail" 在生成控件时没有传递给 ViewData.ModelMetadata.Watermark.为什么?

Does MVC4 by default support placeholders for 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.Watermark while generating control. Why?

型号

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

我得到 html 代码,其中 placeholder 标签没有任何文本

I get html code where placeholder tag do not has any text

<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">

推荐答案

使用插件的替代方法是使用编辑器模板.您需要做的是在SharedEditorTemplates 文件夹中创建一个模板文件,并将其命名为String.cshtml.然后把它放在那个文件中:

An alternative to using a plugin is using an editor template. What you need to do is to create a template file in SharedEditorTemplates folder and call it String.cshtml. Then put this in that file:

@Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue, 
    new { placeholder = ViewData.ModelMetadata.Watermark })

然后像这样在你的视图中使用它:

Then use it in your view like this:

@Html.EditorFor(m=>Model.UnitPercent)

缺点,这适用于 string 类型的属性,并且您必须为每个要支持水印的 type 创建模板.

The downside, this works for properties of type string, and you will have to create a template for each type that you want support for a watermark.

这篇关于MVC4 输入字段占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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