Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

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

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

like image 244
Florent Henry Avatar asked Jan 28 '13 20:01

Florent Henry


People also ask

How can use placeholder in select tag in HTML?

To add a placeholder to a select tag, we can use the <option> element followed by the disabled , selected , and hidden attribute. The disabled attribute makes the option unable to select. The selected attribute shows the text inside <option> element opening and closing tags.

How do you put a placeholder on a tag?

This is used to display the title of the site area or content being retrieved in a menu or navigator. This is for when you create a user name component. You can add a placeholder tag in the design and specify tag="dn" to render the distinguished name of the user.

How do I add a placeholder to a field in HTML?

So putting placeholders in forms in HTML is very simple. You just use the attribute, placeholder and set it equal in single or double quotes to the value that you want it to be.

How can add placeholder in MVC textbox?

you have to use TextBoxFor instead of it try this code: @Html. TextBoxFor(model => model. ToEmail, new {placeholder="Enter Your EmailID...!!!"})


1 Answers

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="">
like image 164
Florent Henry Avatar answered Sep 22 '22 20:09

Florent Henry