2

好的,我已经为字符串定义了一个共享编辑器,如下所示

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

现在我在另一个控件中像这样调用自定义编辑器

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

我的模型是这样的

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

呈现的唯一显示是电子邮件字段。另外两个没有渲染?如果我删除 DataType.Text 和 DataType.Password 那么所有的显示字段都被渲染了??

很奇怪的行为...

有人知道为什么吗?

4

2 回答 2

0

DataType 控制用于呈现的模板类型。当您指定文本或密码时,MVC 将为这些选择默认模板(它们是内置的)并忽略您的模板。

电子邮件之所以有效,是因为没有内置的电子邮件模板,因此它回退到字符串。

编辑:我想我误解了。你是说他们根本不渲染?您的 EditorTemplates 文件夹中是否有空白密码和文本模板?

于 2011-11-29T18:40:14.573 回答
0

您需要一个 ValidationSummary 来显示模型中所有属性的错误。否则,您将需要为模型中的每个属性提供一个 ValidationMessageFor。

这将起作用:

<%= Html.ValidationSummary() %>

或这个:

<%= Html.ValidationMessageFor(model.UserName) %>
<%= Html.ValidationMessageFor(model.Email) %>
<%= Html.ValidationMessageFor(model.Password) %>
于 2011-05-09T21:57:47.710 回答