2
votes

ok i've defined a shared editor for string like the following

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.LabelFor(model => model) %>
<%= Html.TextBoxFor(model => model) %>
<%= Html.ValidationMessageFor(model => model) %>

now i'm calling the custom editor like this in another control

        <%= Html.EditorFor(model=>model.Username)%>
        <%= Html.EditorFor(model=>model.Email)%>
        <%= Html.EditorFor(model=>model.Password)%>

my model is like this

    [Required(ErrorMessage="Le nom d'utilisateur est requis.")]
    [DataType(DataType.Text)]
    [DisplayName("Nom d'utilisateur")]
    public string Username { get; set; }

    [Required(ErrorMessage = "L'email est requis.")]
    [DataType(DataType.EmailAddress)]
    [DisplayName("Courriel")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Le mot de passe est requis.")]
    [ValidatePasswordLength]
    [DataType(DataType.Password)]
    [DisplayName("Mot de passe")]
    public string Password { get; set; }

The only display that is rendered is the Email field. The two others are not rendered ? If i remove the DataType.Text and DataType.Password then all the display fields are rendered ??

Very strange behavior...

Someone knows why ?

2
can you tell my why you need the datatypes?Stefanvds
i could go without but why not?mateo

2 Answers

0
votes

You need a ValidationSummary to show the errors from all properties in a model. Otherwise, you will need a ValidationMessageFor for each property in the model.

This will work:

<%= Html.ValidationSummary() %>

Or this:

<%= Html.ValidationMessageFor(model.UserName) %>
<%= Html.ValidationMessageFor(model.Email) %>
<%= Html.ValidationMessageFor(model.Password) %>
0
votes

DataType controls what type of template is used to render. When you specify Text or Password, then MVC will select the default templates for those (they're built-in) and ignore your template.

Email works because there is no built-in email template, and thus it falls back to string.

EDIT: I think i misunderstood. Are you saying they don't render at all? Do you have blank Password and Text templates in your EditorTemplates folder?