C# 使用 Url.action() 传递动态 javascript 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15112055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing dynamic javascript values using Url.action()
提问by Kokila
Could anyone please tell how to pass dynamic values using Url.action().
谁能告诉我如何使用 Url.action() 传递动态值。
Something Like,
就像是,
var firstname="abc";
var username = "abcd";
location.href = '@Html.Raw(@Url.Action("Display", "Customer", new { uname = firstname ,name = username}))';
firstname, username is not getting reffered inside the Url.action() method.
名字、用户名在 Url.action() 方法中没有被引用。
How to pass these dynamic values using Url.action()?
如何使用 Url.action() 传递这些动态值?
采纳答案by Felipe Oriani
The @Url.Action()
method is proccess on the server-side
, so you cannot pass a client-side
value to this function as a parameter. You can concat the client-side
variables with the server-side
url generated by this method, which is a string on the output. Try something like this:
该@Url.Action()
方法是在 上进行的server-side
,因此您不能将client-side
值作为参数传递给该函数。您可以client-side
使用server-side
此方法生成的url 连接变量,该url 是输出中的字符串。尝试这样的事情:
var firstname = "abc";
var username = "abcd";
location.href = '@Url.Action("Display", "Customer")?uname=' + firstname + '&name=' + username;
The @Url.Action("Display", "Customer")
is processed on the server-side
and the rest of the string is processed on the client-side
, concatenating the result of the server-side
method with the client-side
.
在@Url.Action("Display", "Customer")
上处理 ,server-side
字符串的其余部分在 上处理client-side
,将server-side
方法的结果与 连接起来client-side
。
回答by Oss Silva
In my case it worked great just by doing the following:
在我的情况下,只需执行以下操作即可:
The Controller:
控制器:
[HttpPost]
public ActionResult DoSomething(int custNum)
{
// Some magic code here...
}
Create the form with no action:
创建没有操作的表单:
<form id="frmSomething" method="post">
<div>
<!-- Some magic html here... -->
</div>
<button id="btnSubmit" type="submit">Submit</button>
</form>
Set button click event to trigger submit after adding the action to the form:
设置按钮点击事件在表单中添加动作后触发提交:
var frmSomething= $("#frmSomething");
var btnSubmit= $("#btnSubmit");
var custNum = 100;
btnSubmit.click(function()
{
frmSomething.attr("action", "/Home/DoSomething?custNum=" + custNum);
btnSubmit.submit();
});
Hope this helps vatos!
希望这有助于vatos!
回答by Dash
This answer might not be 100% relevant to the question. But it does address the problem. I found this simple way of achieving this requirement. Code goes below:
此答案可能与问题并非 100% 相关。但它确实解决了问题。我找到了实现这一要求的简单方法。代码如下:
<a href="@Url.Action("Display", "Customer")?custId={{cust.Id}}"></a>
In the above example {{cust.Id}}is an AngularJS variable. However one can replace it with a JavaScript variable.
在上面的例子中{{cust.Id}}是一个 AngularJS 变量。但是,可以用 JavaScript 变量替换它。
I haven't tried passing multiple variables using this method but I'm hopeful that also can be appended to the Url if required.
我还没有尝试过使用这种方法传递多个变量,但我希望如果需要,也可以将其附加到 Url。
回答by Umar Farooq Awan
The easiest way is:
最简单的方法是:
onClick= 'location.href="/controller/action/"+paramterValue'