我遇到了一个问题,即使表单是有效的,表单错误也会在页面上持续存在。
我有以下设置:
_Layout.cshtml:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
</head>
<body>
@RenderBody()
</body>
</html>index.cshtml:
@model SimpleForm.Models.Name
@{
ViewBag.Title = "Form";
}
<script>
$().ready(function () {
$("#quote_form").submit(function () {
return true;
});
});
</script>
@using (Ajax.BeginForm("RunQuote", "Page", new AjaxOptions { UpdateTargetId = "quoteSummary" }, new { id = "quote_form", onreset = "return confirm('Clear all form data?')" }))
{
<input type="submit" value="Send" />
<fieldset>
<legend>Name </legend>
<dl>
<dt>First</dt>
<dd>@Html.TextAreaFor(m => m.first, new { onblur = "$('#quote_form').submit();" })</dd>
<dt>Last</dt>
<dd>@Html.TextAreaFor(m => m.last, new { onblur = "$('#quote_form').submit();" })</dd>
</dl>
</fieldset>
<div class="qMatrixSubText">
@Html.ValidationSummary()
@{
Html.ValidateFor(m => m.first);
Html.ValidateFor(m => m.last);
}
</div>
<div id="quoteSummary" class="quoteSummary">
</div>
}FormController.cs
using System;
using System.Web.Mvc;
using SimpleForm.Models;
namespace SimpleForm.Controllers
{
public class FormController : Controller
{
//
// GET: /Form/
public ActionResult Index()
{
return View();
}
public String RunQuote(Name quote)
{
return "Success!";
}
}
}Name.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace SimpleForm.Models
{
public class Name
{
[Required()]
public string first { get; set; }
public string last { get; set; }
}
}表单填充得很好,如果不首先将任何内容放入其中,并且表单不会提交,我就会看到错误。在首先将某些内容填充到其中之后,我将得到ajax调用,然后它将“成功”发布到div。
然而,这就是问题所在,错误“第一个字段是必需的”。仍然保留在页面上。这就好像ajax提交表单在验证有机会清除错误之前就已经通过了,然后就再也没有机会了。
有办法让验证仍然删除错误吗?
发布于 2011-08-31 03:59:50
没有验证表单,因为您的onblur事件缩短了jquery-unobtrusive-ajax.js中的提交过程。
相反,将两个字段的onblur事件更改为:
$(this).parents('form:first').find(':submit')[0].click(); 这将调用jquery.unobtrusion-ajax.js脚本,导致验证发生在Ajax提交给服务器之前。
更新
当我回顾您的视图时,我看到您正在为您的属性调用@Html.ValidateFor()。这将在@Html.ValidationSummary创建的div之外创建HTML。
由于此标记位于非突出验证所期望的范围之外,因此该标记永远不会被覆盖,并且将始终保持不变。删除@Html.ValidateFor()行。如果您需要在初始加载之后拥有验证消息,请调用document.ready上的after ()来设置您的验证消息。
发布于 2011-09-01 17:30:41
因为我还没有找到正确的解决方案,所以我决定直接针对验证总结错误div,并将类显示:none;添加到li元素中。这隐藏了在提交表单之前未清除的验证错误。
$('#form_errors').children('div.validation-summary-errors').children('ul').children('li').css('display','none')必须有一个更好的解决方案,但我已经尝试了所有的方法,但我不明白为什么我试图强制验证无效。
https://stackoverflow.com/questions/7249703
复制相似问题