C# 如何自动将占位符属性添加到 mvc 4 中的 html 输入类型编号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14570165/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:18:34  来源:igfitidea点击:

How to automatically add placeholder attribute to html input type number in mvc 4?

c#asp.net-mvc-4data-annotationsplaceholdermvc-editor-templates

提问by Florent

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:

这是一个非常具体的问题。我设法使用EmailAddress.cshtml保存在~/Views/Shared/EditorTemplates/文件夹中的名为 的编辑器模板自动将占位符属性添加到 html5 电子邮件输入类型。请参阅下面的代码:

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line", placeholder = ViewData.ModelMetadata.Watermark })

It works because i'm using the [DataType(DataType.EmailAddress)]DataAnnotation in my view model.

它有效是因为我[DataType(DataType.EmailAddress)]在我的视图模型中使用了DataAnnotation。

What doesn't works is when I use a int?variable.

不起作用的是当我使用int?变量时。

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.EditorFortranslates this input like this:

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

The problem is that I can't display the PromptDataAnnotation (usually translated by placeholder). Also, the DataTypeenum doesn't have any "number" or "integer" value that can allow me to use the EditorTemplate like I did for the EmailAddress DataType.

问题是我无法显示PromptDataAnnotation(通常由 翻译placeholder)。此外,DataType枚举没有任何“数字”或“整数”值可以让我像使用 EmailAddress DataType 那样使用 EditorTemplate。

采纳答案by Florent

Based on Pat Burkecomment, I can use the UIHint data attribute combined with the good editor template.

根据Pat Burke 的评论,我可以将 UIHint 数据属性与良好的编辑器模板结合使用。

Here is an example (Editor Template):

下面是一个例子(Editor Template):

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line", placeholder = ViewData.ModelMetadata.Watermark, type = "number" })

(the ViewModel)

( 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; }
}

and finally the result:

最后的结果:

enter image description here

在此处输入图片说明

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