验证码不工作

时间:2012-04-30 12:25:29

标签: asp.net-mvc-3 c#-4.0 razor entity-framework-4.1

我正在以我的一个注册表格

实施验证码

在Example.Cs文件中,我写了以下内容:

public static class CaptchaHelper
 {
  public static string Captcha(this HtmlHelper helper, string text)
       {
           string srcPath = System.Web.VirtualPathUtility.ToAbsolute("~/Handler1.ashx");
           string htmlContent = string.Empty;
           htmlContent += "<script type=\"text/javascript\">function __rc(){document.getElementById(\"" + text +
                          "\").src = \"../Handler1.ashx?query=\" + Math.random();}</script>";
           htmlContent += string.Format("<img id=\"{0}\" src=\"{1}\" alt=\"Captcha Image\"/>", text, srcPath);
           htmlContent += "<a href=\"#\" onclick=\"javascript:__rc();\">Reset</a>";
           return htmlContent;
       }
   }

在View(.cshtml)中,我写了以下内容:

      @Html.Captcha("Sample") 

代替图像,显示脚本可以为此提供任何帮助。

由于 Bhanu

1 个答案:

答案 0 :(得分:1)

试试这个...

public static class CaptchaHelper
 {
  public static MvcHtmlString Captcha(this HtmlHelper helper, string text)
       {
           string srcPath = System.Web.VirtualPathUtility.ToAbsolute("~/Handler1.ashx");
           string htmlContent = string.Empty;
           htmlContent += "<script type=\"text/javascript\">function __rc(){document.getElementById(\"" + text +
                          "\").src = \"../Handler1.ashx?query=\" + Math.random();}</script>";
           htmlContent += string.Format("<img id=\"{0}\" src=\"{1}\" alt=\"Captcha Image\"/>", text, srcPath);
           htmlContent += "<a href=\"#\" onclick=\"javascript:__rc();\">Reset</a>";
           return MvcHtmlString.Create(htmlContent.ToString());
       }
   }

编辑...试试这一行......

htmlContent += string.Format("<img id=" + text + " src=" + srcPath + " alt=\"Captcha Image\"/>", text, srcPath);