简体   繁体   English

Url.Action仅呈现控制器和操作,但不呈现id

[英]Url.Action only render controller and action but not id

I need to render a URL for a JavaScript search that I am doing. 我需要为我正在进行的JavaScript搜索呈现一个URL。 Unfortunately Url.Action renders not only the action but the current id. 不幸的是, Url.Action不仅呈现动作,还呈现当前的id。 This occurs when presently on page utilizing the action with the id. 当目前在页面上利用具有id的动作时,会发生这种情况。

To illustrate Url.Action("List", "Org"); 为了说明Url.Action("List", "Org"); will first render Org/List from which I can append an org to be listed. 将首先渲染Org/List ,我可以从中添加要列出的组织。 However, after the location has been moved to Org/List/12345 Url.Action("List", "Org"); 但是,在将位置移动到Org/List/12345 Url.Action("List", "Org"); will render Org/List/12345 and appending to that creates an issue where I end up with Org/List/12345/6789 . 将渲染Org/List/12345并追加到那会产生一个问题,我最终得到Org/List/12345/6789

Is there a different method I can use other than Url.Action ? 除了Url.Action之外,我还能使用其他方法吗? I've thought about using JavaScript to check for the number of / and removing part of the string but that seems a bit hackish. 我已经考虑过使用JavaScript来检查/删除部分字符串的数量,但这看起来有点像hackish。

// appears in my Site.Master & utilizes the AutoComplete plugin for jQuery
$(document).ready(function() {
    $("input#FindOrg").autocomplete('<%= Url.Action("Find", "Org") %>', {
        minChars: 3,
        maxItemsToShow: 25
    }).result(function(evt, data, formatted) {
        var url = '<%= Url.Action("List", "Org") %>/';
        window.location.href = url + data;
    });
});

Couple of suggestions: 几点建议:

What happens if you use Url.Action("Find", "Org", new { id = "" }) ? 如果您使用Url.Action("Find", "Org", new { id = "" })什么?

Alternately, try manually building the url with Url.Content("~/Find/Org") . 或者,尝试使用Url.Content("~/Find/Org")手动构建URL。

You could 'cheat' and access the 'Controller' and 'Action' entries in the Routing Dictionary. 您可以“欺骗”并访问“路由词典”中的“控制器”和“操作”条目。 This would then allow you to construct only the part of the url you need. 这样就可以只构造你需要的url部分了。 The only caveat is that if you change your routing model these routines may become incorrect. 唯一需要注意的是,如果更改路由模型,这些例程可能会变得不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM