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

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.