我有一个基于MVC 2的网站,其联系表单上有一个反垃圾邮件的CAPTCHA图像。MVC表单帖子刷新图像
完成大部分表单后,用户决定他/她无法读取图像,因此我有一个允许用户使用不同图像刷新页面的按钮。这导致整个页面重新加载(我很好)。
我希望整个页面加载现有的数据保持不变,而只是显示不同的图像。
那么我该如何调用一个保留值并且不会触发验证的动作呢?
现在我遇到的问题是... 1.如何在不触发验证的情况下允许执行帖子? 2.如何维护表单值?
我对AJAX解决方案不感兴趣,我对reCAPTCHA不感兴趣。
谢谢。 Dan。
更新 - 认为我有一个解决方案 - 反馈欢迎!
// All buttons on a form are post to the same action method so read in the value of the submit buttons to take appropriate action.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult sendlink(BusinessLinkModel linkToCreate, string button)
{
// Validation logic
if (string.IsNullOrWhiteSpace(linkToCreate.BusinessTitle)) ModelState.AddModelError("BusinessTitle", "Business title is required.");
string submitButtonName = button;
// If we want to "reload" the captcha image, then we'll clear all the errors, do nothing with the data but update the session so the image
// is re-generated appropriately.
if (submitButtonName == "reload")
{
// Clear the errors - hacktastic!
foreach (ModelState item in this.ModelState.Values.ToList())
{
item.Errors.Clear();
}
ResetSecurityCode();
}
return View();
}
// This is the image url i.e. /home/GenerateSecurityImage
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GenerateSecurityImage()
{
string code = Session["CAPTCHA_Contact"] == null ? ResetSecurityCode() : Session["CAPTCHA_Contact"].ToString();
Response.ContentType = "image/jpeg";
GenerateImage(code, 180, 70).Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
return View();
}
显然,在我的最终版本中,我不会执行验证检查,然后清除它们 - 验证检查只有在实际的最终提交按钮时才会执行。在我循环清理所有错误 - 这就是我紧张的一点 - 即嵌套表单或者我不知道的花哨mvc事物的情况下,微小的点点哈希。另外,我只用了一个星期的MVC,所以我是一个自我承认的n00b! – 2011-01-11 22:05:30