我在一个页面上有2个表单,如下所示:
@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" /> }
解决方法
给提交按钮在您的表单上一个唯一的名称,像这样:
@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","Email") @Html.TextBox("email") <input type="submit" name="login-main" value="Resend" /> }
然后,您可以通过检查对应于提交按钮的密钥的请求值来检查特定表单是否已提交,然后有条件地显示验证摘要。您将添加的顶部形式:
if (Request.Form.AllKeys.Contains("login-top")) { @Html.ValidationSummary() }