动态 Ajax ActionLink RouteValues

发布于 2024-12-03 03:50:26 字数 506 浏览 1 评论 0原文

我有一个 ActionLink 链接

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

,我想在单击时插入路由值“id”。 我想读取的值是一个下拉列表,因此我在 javascript 中得到了类似的内容来获取值:

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

我已经阅读了这篇文章 动态设置 ActionLink 路由值 但我不确定语法应该是什么样子,有人可以帮助我吗?

问候

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神妖 2024-12-10 03:50:26

使用 ajax 选项的 OnBegin 重载。传入的参数之一是请求对象。

您可以从下拉列表中提取值并修改网址。

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

高温TH

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

聚集的泪 2024-12-10 03:50:26

我知道这是一篇很旧的帖子,但我只是在寻找类似的解决方案。根据上面 Slicksim 的回答和 Adam Tuliper 的评论,我使用 data- 属性实现了以下解决方案,以允许重复使用 JavaScript 函数。所以我想我应该发帖来帮助别人。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文