@ Html.ValidationSummary是否适用于MVC3或MVC4中的客户端验证?

时间:2012-02-09 11:40:12

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我有以下代码:

@Html.ValidationSummary(false) 
@Html.ValidationSummary(true, "xxx")

@using (Ajax.BeginForm(
        action,
        "Menus",
        null,
        new AjaxOptions
        {
            UpdateTargetId = "update-message",
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            OnSuccess = success
        }, new { @id = "menuForm" }))
{
    <dl>
    <dt>@Html.LabelFor(model => model.Order)</dt>
    <dd>@Html.TextBoxFor(model => model.Order)</dd>
    <dd>@Html.ValidationMessageFor(model => model.Order)</dd>
    </dl>

当我输入订单值999时,我立即获得客户端验证,在订单文本框之后显示错误消息。但是,Html.ValidationSummary区域中没有显示任何内容。可以在客户端验证中使用它吗?

这是我的模特:

public class Menu
{
    [Range(0, 99, ErrorMessage = "{0} must be between {1} and {2}")]
    [DisplayName("Order")]
    public int Order { get; set; }

我的网络配置:

  <appSettings>
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

1 个答案:

答案 0 :(得分:4)

我不是100%肯定,但我可以在MVC 3默认应用程序中找到的验证摘要在表单中。也许是因为您的验证摘要位于表单之外,因此无法更新。 在表单中移动验证摘要后会发生什么?

自:

@Html.ValidationSummary(false) 
@Html.ValidationSummary(true, "xxx")

@using (Ajax.BeginForm(
        action,
        "Menus",
        null,
        new AjaxOptions
        {
            UpdateTargetId = "update-message",
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            OnSuccess = success
        }, new { @id = "menuForm" }))
{
    <dl>
    <dt>@Html.LabelFor(model => model.Order)</dt>
    <dd>@Html.TextBoxFor(model => model.Order)</dd>
    <dd>@Html.ValidationMessageFor(model => model.Order)</dd>
    </dl>

要:

@using (Ajax.BeginForm(
        action,
        "Menus",
        null,
        new AjaxOptions
        {
            UpdateTargetId = "update-message",
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            OnSuccess = success
        }, new { @id = "menuForm" }))
{
    @Html.ValidationSummary(false) 
    @Html.ValidationSummary(true, "xxx")
    <dl>
    <dt>@Html.LabelFor(model => model.Order)</dt>
    <dd>@Html.TextBoxFor(model => model.Order)</dd>
    <dd>@Html.ValidationMessageFor(model => model.Order)</dd>
    </dl>