Ajax.ActionLink does not trigger controller action

I show a list of files and allow the user to remove from the list. The delete button makes an ajax call to the controller to launch the "Delete" action. However, the delete action is never called. I get a warning defined in AjaxOptions. For what it was worth it, I worked using the WebForms engine and simply transferred it to Razor. This is also the first time I've used the Neighborhoods. If I call the "Delete" action directly, it works. Is this a routing problem?

Here is the code

public EmptyResult Delete(string fileName) { if (fileName.IsNullOrEmpty()) return null; var model = new Models.BenefitBookletModel(); model.DeleteBooklet(fileName); return null; } 

Here is the mark

  @Ajax.ActionLink("test", "Delete", new { fileName = item.FileName }, new AjaxOptions { Confirm = "Are you sure you want to delete " + item.FileName + "?", OnComplete = "function(){deleteComplete('" + item.jsReferenceableFileName + "')}", HttpMethod = "DELETE", OnFailure = "function(){alert('Could not delete file');}" }, new { @class = "DeleteButton" } ) 

Here are my RegisterRoutes

  routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute("SubscriberAll","subscriber/{id}",new { controller = "Subscriber", action = "ShowAll" },new { id = @"\d+" } ); routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); 

Routes from the registration area

 context.MapRoute("Marketing_default","Marketing/{controller}/{action}/{id}",new { action = "Index", id = UrlParameter.Optional } 

Here is the markup created

 <a href="/Marketing/BenefitBooklet/Delete?fileName=MyFileName.pdf" data-ajax-method="GET" data-ajax-failure="function(){alert('Could not delete file');}" data-ajax-confirm="Are you sure you want to delete MyFileName.pdf?" data-ajax-complete="function(){deleteComplete('MyFileName.pdf')}" data-ajax="true" class="DeleteButton"> </a> 
+4
source share
2 answers

You must specify the function name as the value of the corresponding AjaxOptions property. Add a script section:

 <script type="text/javascript"> function OnFailure(request, error) { alert('Could not delete file'); } function OnComplete(request, error) { alert('Delete complete'); } </script> 

in your view and change OnFailure and OnComplete to 'AjaxOptions':

 OnFailure = "OnFailure" OnComplete= "OnComplete" 
+4
source

You define the HttpMethod action reference as DELETE, but your method probably only accepts GET. Try to decorate it with the verb "Delete action".

 [HttpDelete] public EmptyResult Delete(string fileName) 
+1
source

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


All Articles