5

我有以下代码:

@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>
4

1 回答 1

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>
于 2012-02-09T13:15:08.813 回答