0

I'm changing the link of an image on the client side and it invokes the controller asynchronously.

I don't understand this behavior, could someone explain me what is happening?

Why url adding actually invokes the controller by default, moreover it happens asynchronously.

    <script>
        $("#refreshLnk").click(function () {
            $("#cap").attr('src', '@Url.Action("CaptchaImage")?' + new Date().getTime());
        });
    </script>

   <img id="cap" alt="Captcha" src="@Url.Action("CaptchaImage")" style="" />
   <a id="refreshLnk" href="#">refresh</a>

   public ActionResult CaptchaImage()
   {
   }
4

1 回答 1

2

这是正常和预期的行为。当您单击该按钮时,您的脚本会将图像的 src 属性更改为类似于“/CaptchaImage?1376967614675”的内容。浏览器尝试渲染图像,因此调用此 url。这会触发控制器中的 CaptchaImage 方法。

与你的问题相关的帖子很多,可以从这一篇开始:Url.Action 是如何工作的 Asp.net MVC?. 希望能帮助到你!

于 2013-08-20T03:09:00.730 回答