如何在 ASP.NET MVC 5 中使用 JavaScript 在运行时使用路由值增加 actionlink?
How to augment actionlink with route values at runtime with JavaScript in ASP.NET MVC 5?
我有这个 ActionLink。
@Html.ActionLink("Link", "action", "controller", null, htmlAttributes: new {@class = "menuLink"})
我必须将 routeValues 设置为 null
因为我不知道编译时的值。它们是在运行时从某些下拉菜单的选定值中接收到的。
因此,我试图在运行时使用 JavaScript 增加路由值。我看不出还有什么选择。
我使用 ActionLink 上的 CSS class menuLink
来捕捉事件。
$(".menuLink").click(function () {
var $self = $(this),
routeValue1 = getFromDropdown1(),
routeValue2 = getFromDropdown2(),
href = $self.attr("href");
// ???
});
如何添加路由值以使 href 正确?
我希望 URL 是这样的:
http://localhost/mySite/controller/action/2/2
我的重点是 /2/2
部分..
我试过了
$self.attr("foo", routeValue1);
$self.attr("bar", routeValue2);
但后来我得到一个 URL 这样的:
http://localhost/mySite/controller/action?foo=2&bar=2
它不适用于我的 routes.MapeRoute
routes.MapRoute(
name: "Authenticated",
url: "{controller}/{action}/{foo}/{bar}",
defaults: new { controller = "Home", action = "WelcomePage", Foo = "0", Bar = "0" }
);
不知道是不是Foo = "0", Bar = "0"
的问题
已添加解决方案。感谢@Zabavsky
if (!String.format) {
String.format = function (format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
var href = $self.attr("href");
var hrefDecoded = decodeURIComponent(href);
var hrefFormatted = String.format(hrefDecoded, 2, 1); // 2 and 1 test only..
$self.attr('href', hrefFormatted);
您可以像下面这样动态更改锚标记的 href 属性。
$(".menuLink").click(function () {
var $self = $(this),
routeValue1 = getFromDropdown1(),
routeValue2 = getFromDropdown2(),
href = $self.attr("href"),
editedHref = href.split('?')[0];
$self.attr('href', editedHref+'/'+ routeValue1 +'/'+routeValue2 );
});
我不确定这是最好的方法,但我会为您提供解决此问题的方法。主要思想是生成具有所有必需参数的 link 并使用 javascript 格式化 href
,就像 c# 中的 string.Format
。
- Generate the link:
@Html.ActionLink("Link", "action", "controller",
new { Foo = "{0}", Bar = "{1}"}, htmlAttributes: new { @class = "menuLink" })
助手将生成正确的 url,根据您的路由配置,它看起来像这样:
<a href="http://website.com/controller/action/{0}/{1}" class="menuLink">Link</a>
- Write the
format
function.
您可以查看如何实现一个 here. If you are using jQuery validation plugin you can utilize the built in format function。
- Format
href
attribute in your javascript:
$(".menuLink").click(function () {
var routeValue1 = getFromDropdown1(),
routeValue2 = getFromDropdown2(),
url = decodeURIComponent(this.href);
// {0} and {1} will be replaced with routeValue1 and routeValue1
this.href = url.format(routeValue1, routeValue2);
});
不要像这样将路由参数连接到 href href +'/'+ routeValue1 +'/'+routeValue2
,如果您更改路由配置,url 将导致 404。您应该始终使用 Url 助手生成 urls 并避免在 javascript 代码中对它们进行 hadcoding。
我有这个 ActionLink。
@Html.ActionLink("Link", "action", "controller", null, htmlAttributes: new {@class = "menuLink"})
我必须将 routeValues 设置为 null
因为我不知道编译时的值。它们是在运行时从某些下拉菜单的选定值中接收到的。
因此,我试图在运行时使用 JavaScript 增加路由值。我看不出还有什么选择。
我使用 ActionLink 上的 CSS class menuLink
来捕捉事件。
$(".menuLink").click(function () {
var $self = $(this),
routeValue1 = getFromDropdown1(),
routeValue2 = getFromDropdown2(),
href = $self.attr("href");
// ???
});
如何添加路由值以使 href 正确?
我希望 URL 是这样的:
http://localhost/mySite/controller/action/2/2
我的重点是 /2/2
部分..
我试过了
$self.attr("foo", routeValue1);
$self.attr("bar", routeValue2);
但后来我得到一个 URL 这样的:
http://localhost/mySite/controller/action?foo=2&bar=2
它不适用于我的 routes.MapeRoute
routes.MapRoute(
name: "Authenticated",
url: "{controller}/{action}/{foo}/{bar}",
defaults: new { controller = "Home", action = "WelcomePage", Foo = "0", Bar = "0" }
);
不知道是不是Foo = "0", Bar = "0"
的问题
已添加解决方案。感谢@Zabavsky
if (!String.format) {
String.format = function (format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
var href = $self.attr("href");
var hrefDecoded = decodeURIComponent(href);
var hrefFormatted = String.format(hrefDecoded, 2, 1); // 2 and 1 test only..
$self.attr('href', hrefFormatted);
您可以像下面这样动态更改锚标记的 href 属性。
$(".menuLink").click(function () {
var $self = $(this),
routeValue1 = getFromDropdown1(),
routeValue2 = getFromDropdown2(),
href = $self.attr("href"),
editedHref = href.split('?')[0];
$self.attr('href', editedHref+'/'+ routeValue1 +'/'+routeValue2 );
});
我不确定这是最好的方法,但我会为您提供解决此问题的方法。主要思想是生成具有所有必需参数的 link 并使用 javascript 格式化 href
,就像 c# 中的 string.Format
。
- Generate the link:
@Html.ActionLink("Link", "action", "controller",
new { Foo = "{0}", Bar = "{1}"}, htmlAttributes: new { @class = "menuLink" })
助手将生成正确的 url,根据您的路由配置,它看起来像这样:
<a href="http://website.com/controller/action/{0}/{1}" class="menuLink">Link</a>
- Write the
format
function.
您可以查看如何实现一个 here. If you are using jQuery validation plugin you can utilize the built in format function。
- Format
href
attribute in your javascript:
$(".menuLink").click(function () {
var routeValue1 = getFromDropdown1(),
routeValue2 = getFromDropdown2(),
url = decodeURIComponent(this.href);
// {0} and {1} will be replaced with routeValue1 and routeValue1
this.href = url.format(routeValue1, routeValue2);
});
不要像这样将路由参数连接到 href href +'/'+ routeValue1 +'/'+routeValue2
,如果您更改路由配置,url 将导致 404。您应该始终使用 Url 助手生成 urls 并避免在 javascript 代码中对它们进行 hadcoding。