Eslam Soliman
Eslam Soliman

Reputation: 1296

Call MVC action using java script within a view

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

Upvotes: 0

Views: 503

Answers (1)

alexn
alexn

Reputation: 58952

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.

Upvotes: 3

Related Questions