在视图中使用 java 脚本调用 MVC 操作

发布于 2024-12-01 18:56:48 字数 618 浏览 3 评论 0原文

我有一个包含图像的视图,我想设置一个按钮来通过调用我的控制器中的操作来重新加载该图像,如下所示:

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

以及如下操作:

public ActionResult Image()
        {
            var builder = new XCaptcha.ImageBuilder();
            var result = builder.Create();

            Session.Add("SecurityNumber", result.Solution);

            return new FileContentResult(result.Image, result.ContentType);
        }

我需要使用 javascript 调用此操作并设置新的图像源任何帮助会好的

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人疚 2024-12-08 18:56:48

要更新图像,只需更新 src 属性即可。使用 jQuery:

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

    e.preventDefault();
});

它的作用是,当您单击 id 为“Reload”的元素时,将 id 为“Captcha”的图像的“src”属性设置为“/path/of/your/action”。 e.preventDefault() 就在那里,所以我们不点击该链接。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文