Dynamic Ajax ActionLink RouteValues

I have an ActionLink this link

@Ajax.ActionLink("Delete it!", "Delete", new {id = getTheID}, new AjaxOptions { Confirm = "Really?", HttpMethod = "Delete", UpdateTargetId = "ddlRoles" }) 

and I want to insert the route id value on click. The value I want to read is a dropdown, so I got something like this in javascript to get the value:

 $('#ddlRoles :selected').val() 

I already read this post dynamically sets ActionLink routeValues ​​but I'm not sure what the syntax should look like, can someone help me?

Hello

+4
source share
2 answers

Use OnBegin overloading ajax options. One of the parameters passed to this request object.

You can pull the value from the drop-down list and change the URL.

 function onBegin(xhr, request) { request.url = "@Url.Action("SomeAction", "SomeController")/" + $("ddl").val(); } 

NTN

Si

+3
source

I know this is a pretty old post, but I was just looking for a similar solution. Based on Slicksim's answer above and a comment from Adam Tulper, I applied the solution below using the data attribute to use a repeating javascript function. So I thought I would send to help others.

 @Ajax.ActionLink("Click Me", "ActionName", null, new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "TargetID", OnBegin = "updateHref" }, new { data_dependentid = "DependentFieldName" }) <script> function updateHref(xhr, request) { var requester = $(this); var dependentid = $('#' + requester.attr('data-dependentid')).val(); var requestParams = request.url.split('?'); request.url = requestParams[0] + '/' + dependentid + '?' + requestParams[1]; } </script> 
0
source

All Articles