0

我正在我的一种注册表单中实施验证码

在 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;
       }
   }

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

      @Html.Captcha("Sample") 

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

谢谢巴努

4

1 回答 1

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);
于 2012-04-30T12:45:56.057 回答