Asp.net mvc MVC表单post刷新图像
我有一个基于MVC2的网站,上面有一个带有反垃圾邮件验证码图像的联系表单 完成大部分表单后,用户决定他/她无法读取图像,因此我有一个按钮,允许用户使用不同的图像刷新页面。这会导致整个页面重新加载(我对此很满意) 我希望加载现有数据的整个页面保持不变,只显示不同的图像 那么,如何调用保留值且不触发验证的操作呢 现在我的问题是。。。 1.如何允许在不触发验证的情况下执行post? 2.如何维护表单值 我对AJAX解决方案不感兴趣,对reCAPTCHA也不感兴趣 谢谢。 丹 更新-认为我有解决方案-欢迎反馈强>Asp.net mvc MVC表单post刷新图像,asp.net-mvc,image,refresh,Asp.net Mvc,Image,Refresh,我有一个基于MVC2的网站,上面有一个带有反垃圾邮件验证码图像的联系表单 完成大部分表单后,用户决定他/她无法读取图像,因此我有一个按钮,允许用户使用不同的图像刷新页面。这会导致整个页面重新加载(我对此很满意) 我希望加载现有数据的整个页面保持不变,只显示不同的图像 那么,如何调用保留值且不触发验证的操作呢 现在我的问题是。。。 1.如何允许在不触发验证的情况下执行post? 2.如何维护表单值 我对AJAX解决方案不感兴趣,对reCAPTCHA也不感兴趣 谢谢。 丹 更新-认为我有解决方案-欢
// 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();
}
如果你真的要夺回,你必须为此制定一项特别行动
public ActionResult Recapture(Model m)
{
// reinit capture
return View("yourForm", m);
}
yourForm—最初显示给用户的表单。如果用一些值初始化m,它们将显示在表单上。如果未调用Model.IsValid,则不会导致验证。我认为这仍然会触发验证,因为模型将被注释。但我很喜欢你把数据传回去的方式!天才我会马上回复-希望得到关于它有多黑客的反馈。显然,在我的最终版本中,我不会执行验证检查,然后清除它们-只有当它是实际的最终提交按钮时,才会执行验证检查。在我循环清除所有错误的时候有一点不对劲-这是我担心的一点-例如,在嵌套表单或我不知道的花式mvc的情况下。而且,我只使用MVC一周,所以我是一个自认的n00b!