开发者

asp mvc 2 EditorFor strange behavior

开发者 https://www.devze.com 2023-01-12 13:54 出处:网络
ok i\'ve defined a shared editor for string like the following <%@ Control Language=\"C#\" Inherits=\"System.Web.Mvc.ViewUserControl\"

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 ?


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号