问题描述
这是一个非常具体的问题.我设法使用保存在~/Views/Shared/EditorTemplates/
文件夹中的名为EmailAddress.cshtml
的编辑器模板将占位符属性自动添加到html5电子邮件输入类型中.请参见下面的代码:
This is a very specific issue. I managed to automatically add the placeholder attribute to html5 email input type by using an editor template called EmailAddress.cshtml
, saved in ~/Views/Shared/EditorTemplates/
folder. See the code below:
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line", placeholder = ViewData.ModelMetadata.Watermark })
之所以有效,是因为我在视图模型中使用了[DataType(DataType.EmailAddress)]
DataAnnotation.
It works because i'm using the [DataType(DataType.EmailAddress)]
DataAnnotation in my view model.
不起作用的是当我使用int?
变量时.
What doesn't works is when I use a int?
variable.
public class MiageQuotaRequestViewModel
{
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Nombre de place demandées", Prompt = "Nombre de place")]
[Range(0, 50, ErrorMessage = "La demande doit être comprise entre 0 et 50 places")]
public int? RequestedQuota { get; set; }
}
@Html.EditorFor
像这样翻译此输入:
<input class="text-box single-line" data-val="true" data-val-number="The field Nombre de place demandées must be a number." data-val-range="La demande doit être comprise entre 0 et 50 places" data-val-range-max="50" data-val-range-min="0" data-val-required="Le champ Nombre de place demandées est requis." id="RequestedQuota" name="RequestedQuota" type="number" value="">
问题是我无法显示Prompt
DataAnnotation(通常由placeholder
翻译).另外,DataType
枚举没有任何数字"或整数"值,可以让我像对EmailAddress数据类型那样使用EditorTemplate.
The problem is that I can't display the Prompt
DataAnnotation (usually translated by placeholder
). Also, the DataType
enum doesn't have any "number" or "integer" value that can allow me to use the EditorTemplate like I did for the EmailAddress DataType.
推荐答案
基于 Pat Burke 评论,我可以结合使用UIHint数据属性和良好的编辑器模板.
Based on Pat Burke comment, I can use the UIHint data attribute combined with the good editor template.
以下是示例(Editor Template
):
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line", placeholder = ViewData.ModelMetadata.Watermark, type = "number" })
(ViewModel
)
public class MiageQuotaRequestViewModel
{
[Required]
[UIHint("Number")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Nombre de place demandées", Prompt = "Nombre de place")]
[Range(0, 50, ErrorMessage = "La demande doit être comprise entre 0 et 50 places")]
public int? RequestedQuota { get; set; }
}
最后是结果:
<input class="text-box single-line"
data-val="true"
data-val-number="The field Nombre de place demandées must be a number."
data-val-range="La demande doit être comprise entre 0 et 50 places"
data-val-range-max="50"
data-val-range-min="0"
data-val-required="Le champ Nombre de place demandées est requis."
id="RequestedQuota"
name="RequestedQuota"
placeholder="Nombre de place"
type="number"
value="">
这篇关于如何在MVC 4中自动将占位符属性添加到html输入类型编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!