在多个表单上指定验证摘要

Sha*_*ean 28 asp.net-mvc asp.net-mvc-3

我在页面上有两个表单如下:

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    @Html.Label("code", "Confirmation Code")
    @Html.TextBox("code")
    <input type="submit" value="Go" />
}
@using (Html.BeginForm("SendConfirmation", "Auth"))
{
    @Html.ValidationSummary()
    @Html.Label("email", "Email")
    @Html.TextBox("email")
    <input type="submit" value="Resend" />
}
Run Code Online (Sandbox Code Playgroud)

如果SendConfirmation抛出错误,则显示2个验证摘要.如何获得验证摘要以定位自己的?

Dan*_*plo 20

在表单上为提交按钮指定一个唯一的名称,如下所示:

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    @Html.Label("code", "Confirmation Code")
    @Html.TextBox("code")
    <input type="submit" name="login-top" value="Go" />
}
@using (Html.BeginForm("SendConfirmation", "Auth"))
{
    @Html.ValidationSummary()
    @Html.Label("email", "Email")
    @Html.TextBox("email")
    <input type="submit" name="login-main" value="Resend" />
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过检查与提交按钮对应的键的请求值来检查是否已提交特定表单,然后有条件地显示验证摘要,即.在顶部形式,您将添加:

if (Request.Form.AllKeys.Contains("login-top"))
{
    @Html.ValidationSummary()
}
Run Code Online (Sandbox Code Playgroud)


Man*_*een 6

解决方案是仅在验证表单时绘制验证摘要

有关详细信息,请查看此博客文章