Dynamic Ajax ActionLink RouteValues

别等时光非礼了梦想. 提交于 2019-12-10 15:37:48

问题


I have an ActionLink link this

@Ajax.ActionLink("Delete it!", "Delete", 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


回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/7334309/dynamic-ajax-actionlink-routevalues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!