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

后端 未结 1 2058
醉梦人生
醉梦人生 2021-02-08 22:12

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

相关标签:
1条回答
  • 2021-02-08 22:28

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

    Here is an example (Editor Template):

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

    (the 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="">
    
    0 讨论(0)
提交回复
热议问题