将Url.Action与动态值一起使用

时间:2016-01-29 10:42:45

标签: javascript asp.net-mvc

我有以下链接

var selectedValue = "someDynamicValue";
var selectedId = 100;
window.location.href = '/Home/' + selectedValue + '/' + selectedId;

如何使用@Url.Action而不是上面的链接生成?

1 个答案:

答案 0 :(得分:0)

我通常这样做:

var selectedValue = "someDynamicValue";
var selectedId = 100;
var href = '@Url.Action("Index", "Home", new { selectedValue = "_selectedValue_", selectedId = "_selectedId_"})';
href = href.replace('_selectedValue_', selectedValue);
href = href.replace('_selectedId_', selectedId);
window.location.href = href;