jQuery / ASP.Net MVC将id变量传递给content.load

时间:2022-11-30 17:42:55

This will probably be a rather simple question.

这可能是一个相当简单的问题。

How do I pass:

我如何通过:

var ident = $(this).attr("id");

var ident = $(this).attr(“id”);

to

content.load("@Html.Raw(Url.Action("Action", "Controller", new {id=ident}))");

content.load(“@ Html.Raw(Url.Action(”Action“,”Controller“,new {id = ident}))”);

I've tried:

... new {id="+ident+"}))"); but it doesn't work.

... new {id =“+ ident +”}))“);但它不起作用。

Solution

For my specific problem, the best solution I found was the following:

对于我的具体问题,我找到的最佳解决方案如下:

var ident = $(this).attr("id");
var route = "@Html.Raw(Url.Action("Action", "Controller"))";
var url = addParameterToURL(route, ident);
content.load(url);

With the method:

用方法:

function addParameterToURL(url, param) {
    _url = url;
    _url += (_url.split('/')[1] ? '/' : '/') + param;
    return _url;
}

1 个解决方案

#1


0  

Please feel the difference here between server-side code and client-side code. This:

请注意服务器端代码和客户端代码之间的区别。这个:

var ident = $(this).attr("id");

is javascript, it is executed on the client's page. While this:

是javascript,它在客户端的页面上执行。虽然这个:

"@Html.Raw(Url.Action("Action", "Controller"))"

Is a server-side code, that is executed on the server way before page goes to the client. You simply cannot mix the two with the things like concatenation.

是服务器端代码,在页面进入客户端之前在服务器上执行。你根本无法将这两者与串联之类的东西混合在一起。

The easiest way for you would be to store the result of server-side execution into some variable, and then add a param to it. Store it like this:

最简单的方法是将服务器端执行的结果存储到某个变量中,然后为其添加一个参数。像这样存储:

var url = "@Html.Raw(Url.Action("Action", "Controller"))";

And then, while in JS, add a parameter to it. Use this function for example (just make sure to make an url as a param, rather than using current location).

然后,在JS中,添加一个参数。例如,使用此函数(只需确保将URL作为参数,而不是使用当前位置)。

#1


0  

Please feel the difference here between server-side code and client-side code. This:

请注意服务器端代码和客户端代码之间的区别。这个:

var ident = $(this).attr("id");

is javascript, it is executed on the client's page. While this:

是javascript,它在客户端的页面上执行。虽然这个:

"@Html.Raw(Url.Action("Action", "Controller"))"

Is a server-side code, that is executed on the server way before page goes to the client. You simply cannot mix the two with the things like concatenation.

是服务器端代码,在页面进入客户端之前在服务器上执行。你根本无法将这两者与串联之类的东西混合在一起。

The easiest way for you would be to store the result of server-side execution into some variable, and then add a param to it. Store it like this:

最简单的方法是将服务器端执行的结果存储到某个变量中,然后为其添加一个参数。像这样存储:

var url = "@Html.Raw(Url.Action("Action", "Controller"))";

And then, while in JS, add a parameter to it. Use this function for example (just make sure to make an url as a param, rather than using current location).

然后,在JS中,添加一个参数。例如,使用此函数(只需确保将URL作为参数,而不是使用当前位置)。