Yury
Yury

Reputation: 57

jquery update captcha

I trying to update captcha with js and ASP.NET MVC3 razor. I found this solution: View:

<div id ="CaptchaDiv"> <img alt="Captcha" id="Captchaimg" src="@Url.Action("GetCaptcha")" style="" /></div>  <input type="button"  value="Refresh" onclick=" return GetCaptcha()" />

Controller:

public ActionResult GetCaptcha ()
        {
            FileContentResult img = null;
            int res = 0;
            MemoryStream mem = Metods.CaptchaImage( out res);
            Session["Captcha"] = res;
          img = this.File(mem.GetBuffer(), "image/Jpeg");
            return img;
        }

JS:

function GetCaptcha()
 {
     $('Captchaimg').attr('src', '/Account/GetCaptcha?' + new Date().getTime());
 }

But: it doesn't work. JS runs successfuly. But controller action doesn't run. What is wrong? Any ideas?

Upvotes: 0

Views: 1144

Answers (1)

actimel
actimel

Reputation: 451

I think that your selector $('Captchaimg') must be wrong. If you're using id, it should be $('#Captchaimg').

Also what does trigger this action? I'd use:

$('#Captchaimg').click(function(){
    $(this).attr('src', '/Account/GetCaptcha?' + new Date().getTime());
});

And I'd change id's to lowercase.

Upvotes: 3

Related Questions