Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

an Ajax.ActionLink fired only the first time

I have an ActionLink in my view. When clicked, an action in my controller is fired (I placed a breakpoint to check). It works only the first time the link is clicked. Other times, the breakpoint in my action controller is never reached.

@Ajax.ActionLink("Remove image", "RemoveImage", new { projectID = Model.ProjectID }, new AjaxOptions { OnSuccess="ImageRemovedSuccess" })   

The function "ImageRemovedSuccess" for the OnSuccess ajax event is well fired but the action in the controller is not fired.

Any suggestions?

Thanks.

like image 878
Bronzato Avatar asked Nov 19 '11 10:11

Bronzato


2 Answers

Your browser caches the response. So the second time when you send the request browser will not forward it to the server because the same request was sent to the server before and the browser will return the cached response to the page.

Decorate the action method not to cache the response in between.

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult RemoveImage(int projectID)
    {

    }

Make sure you press "Ctrl + F5" on the browser before you check it.

like image 55
Eranga Avatar answered Oct 21 '22 03:10

Eranga


Additional answer for Partial Views (only)

Partial views don't like the settings NoStore or a Duration of 0.

You get Additional information: OutputCacheAttribute for child actions only supports Duration, VaryByCustom, and VaryByParam values. Please do not set CacheProfile, Location, NoStore, SqlDependency, VaryByContentEncoding, or VaryByHeader values for child actions. for NoStore and another error that Duration must be positive if you set Duration=0

The fix for Partial views (compatible with the normal views) is:

   [OutputCache(Duration = 1, VaryByParam = "*")]

This caches for a maximum of one second, which should be fine for normal operations.

like image 44
Gone Coding Avatar answered Oct 21 '22 05:10

Gone Coding