Ajax.ActionLink only fired for the first time

I have ActionLink in my opinion. When you click, the action in my controller starts (I set a breakpoint for verification). It only works the first time you click a link. In other cases, the breakpoint in my action controller is never reached.

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

The "ImageRemovedSuccess" function for the ajax OnSuccess event works well, but the action in the controller does not start.

Any suggestions?

Thanks.

+4
source share
2 answers

Your browser caches the response. Thus, the second time you send the request browser, it will not redirect it to the server, because the same request was sent to the server earlier, and the browser will return the cached response to the page.

Decorate the action method so as not to cache the response between them.

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

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

+16
source

Additional answer for partial views (only)

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

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. 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

Fix for partial views (compatible with normal views):

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

This is cached for no more than one second, which should be normal for normal operations.

+1
source

Source: https://habr.com/ru/post/1382031/


All Articles