如何将模型中的占位符文本添加到MVC视图中? [英] How do I add placeholder text from the model into a MVC view?

查看:102
本文介绍了如何将模型中的占位符文本添加到MVC视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型:

[DataType(DataType.EmailAddress)]
[DisplayFormat(ConvertEmptyStringToNull = true)]
[Display(Prompt = "Email Address")]
public string Email { get; set; }

我试图让提示显示在结果文本框的占位符文本中具有以下内容:

I am trying to get the "prompt" to show in the placeholder text of the resulting text box with the following:

@Html.EditorFor(model => model.Email, 
new { htmlAttributes = new { @class = "form-control input-md",
placeholder = @ViewData.ModelMetadata.Watermark } })

当我查看生成的HTML时,我只在输入标记中获得占位符。根据我所读的ViewData.ModelMetadata.Watermark应该工作。获取此占位符文本的正确方法是什么?

When I view the generated HTML I only get "placeholder" in the input tag. According to what I have read ViewData.ModelMetadata.Watermark should work. What is the correct way to get this placeholder text in place?

推荐答案

这解决了我的问题:

@Html.EditorFor(model => model.Email, new { htmlAttributes = 
new { @class = "form-control input-sm", 
placeholder = @Html.DisplayNameFor(m=>m.Email) } })

这样做的代码是

placeholder = @Html.DisplayNameFor(m=>m.Email) 

这篇关于如何将模型中的占位符文本添加到MVC视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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