Dynamic Ajax ActionLink RouteValues
I have an ActionLink link this
@Ajax.ActionLink("Delete it!", "Del开发者_StackOverflow社区ete", new {id = getTheID}, new AjaxOptions { Confirm = "Really?", HttpMethod = "Delete", UpdateTargetId = "ddlRoles" })
and I want to insert the route value "id" on click. The value I want to read from is a drop-down list, so I got something like this in javascript to get the value:
$('#ddlRoles :selected').val()
I already read this post set ActionLink routeValues dynamically but I'm not sure how the syntax should look like, can someone help me?
Regards
Use the OnBegin overload of the ajax options. One of the parameters passed in is the request object.
You could pull the value out of the dropdownlist there and amend the Url.
function onBegin(xhr, request)
{
request.url = "@Url.Action("SomeAction", "SomeController")/" + $("ddl").val();
}
HTH
Si
I know this is a pretty old post, but I was just looking for a similar solution. Based on Slicksim's answer above and the comment from Adam Tuliper, I implemented the below solution using the data- attribute to allow for a repeatably used javascript function. So I thought I would post 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>
精彩评论