MVC验证:显示错误消息

时间:2013-08-14 21:52:27

标签: validation asp.net-mvc-4

嗨,这是我的模型代码中的RegularExpression

    [Required]
    [StringLength(127)]
    [RegularExpression("^[a-zA-Z]+$", ErrorMessage = "Enter only alphabets for First Name")]
    public string FirstName { get; set; } //First Name should only use Alphabets

这是/home/index.aspx页面中的验证

<%  using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "AudienceRequest" }))
    { %>
        <%= Html.ValidationSummary(true, "To request tickets, please complete the required (*) fields below and click Submit") %>


        <div>
            <label>First Name
            <%= Html.ValidationMessageFor(x => x.FirstName, "*") %></label>
            <%= Html.TextBoxFor(x => x.FirstName) %>

目前,验证部分有效。如果我输入BOB333,它将不会提交表格。但显示的消息是错误的。它仍然显示“要请求票证,请填写下面所需的(*)字段,然后单击”提交“,而不是”仅输入名字的字母“

请告知。

由于

1 个答案:

答案 0 :(得分:3)

您需要更改

<%= Html.ValidationSummary(true, "To request tickets, please complete the required (*) fields below and click Submit") %>

<%= Html.ValidationSummary(false, "To request tickets, please complete the required (*) fields below and click Submit") %>

布尔标志是“排除属性错误” - 请参阅MSDN