0

I have a view that contain a image i want to set a button to reload this image by calling an action in my controller like this :

<a href="javascript://" class="Button ButtonLarge" id="Reload"><span><span class="ButtonIcon NewTopic">Refresh</span></span></a>

and the action like this:

public ActionResult Image()
 {
 var builder = new XCaptcha.ImageBuilder();
 var result = builder.Create();
 Session.Add("SecurityNumber", result.Solution);
 return new FileContentResult(result.Image, result.ContentType);
 }

i need to call this action using javascript and set the new image source any help will be good

asked Aug 29, 2011 at 9:17
4
  • you can use jquery $.get . api.jquery.com/jQuery.get instead of using javascript Commented Aug 29, 2011 at 9:20
  • I need it to be using javascript Commented Aug 29, 2011 at 9:22
  • or if you can give me an exampl of how can i do it in jquerey Commented Aug 29, 2011 at 9:24
  • yes I am not verey good with it Commented Aug 29, 2011 at 9:27

1 Answer 1

3

To update an image, just update the src attribute. Using jQuery:

$("#Reload").click(function(e) {
 $("#Captcha").attr("src", "/path/to/your/action");
 e.preventDefault();
});

What this does is when you click an element with id "Reload", set the "src" attribute of the image with id "Captcha" to "/path/of/your/action". e.preventDefault() is there so we do not follow the link.

answered Aug 29, 2011 at 9:26
4
  • <script type="text/javascript"> $(document).ready(function () { $("#Reload").click(function (e) { $("#image").attr("src", "/AccountController/Image"); e.preventDefault(); }); )}; </script> Commented Aug 29, 2011 at 9:31
  • @Eslam Yes, that should work. Are you sure you want the Controller suffix in there? I guess it should be "/Account/Image". Commented Aug 29, 2011 at 9:32
  • thanks a lot alexn ,and i have a nother question can i made it without reloading the page Commented Aug 29, 2011 at 9:37
  • it reloads the image only one time is there is something in what i wrote Commented Aug 29, 2011 at 9:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.