使用Url.action()传递动态javascript值

Kok*_*ila 56 javascript c# asp.net asp.net-mvc asp.net-core

任何人都可以告诉我们如何使用Url.action()传递动态值.

就像是,

var firstname="abc";
var username = "abcd";
location.href = '@Html.Raw(@Url.Action("Display", "Customer", new { uname = firstname ,name = username}))';
Run Code Online (Sandbox Code Playgroud)

firstname,在Url.action()方法中没有得到用户名.

如何使用Url.action()传递这些动态值?

Fel*_*ani 124

@Url.Action()方法是进程server-side,因此您不能将client-side值作为参数传递给此函数.您可以client-side使用server-side此方法生成的url连接变量,该方法是输出中的字符串.尝试这样的事情:

var firstname = "abc";
var username = "abcd";
location.href = '@Url.Action("Display", "Customer")?uname=' + firstname + '&name=' + username;
Run Code Online (Sandbox Code Playgroud)

对其@Url.Action("Display", "Customer")进行处理server-side,并对其余的字符串进行处理client-side,将server-side方法的结果与client-side.


小智 5

此答案可能与问题并非100%相关。但这确实解决了这个问题。我找到了实现此要求的简单方法。代码如下:

<a href="@Url.Action("Display", "Customer")?custId={{cust.Id}}"></a>
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,{{cust.Id}}是AngularJS变量。但是,可以将其替换为JavaScript变量。

我没有尝试使用此方法传递多个变量,但希望可以将其附加到Url(如果需要)。