剃刀视图中的 Javascript url 操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11599995/
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
Javascript url action in razor view
提问by nebula
I have a javascript method onRowSelected
wchich gets rowid. How to pass the rowid in certain action of a controller with HttpGet
?
我有一个 javascript 方法onRowSelected
wchich 获取 rowid。如何在控制器的某些操作中传递 rowid HttpGet
?
function onRowSelected(rowid, status) {
alert('This row has id: ' + rowid);
//url: @Action.Url("Action","Controller")
//post:"GET"
// Something like this?
}
回答by Darin Dimitrov
If your controller action expects an id query string parameter:
如果您的控制器操作需要 id 查询字符串参数:
var url = '@Url.Action("Action", "Controller")?id=' + rowid;
or if you want to pass it as part of the route you could use replace:
或者如果您想将其作为路线的一部分传递,您可以使用替换:
var url = '@Url.Action("Action", "Controller", new { id = "_id_" })'
.replace('_id_', rowid);
yet another possibility if you are going to send an AJAX request is to pass it as part of the POST body:
如果您要发送 AJAX 请求,另一种可能性是将其作为 POST 正文的一部分传递:
$.ajax({
url: '@Url.Action("Action", "Controller")',
type: 'POST',
data: { id: rowid },
success: function(result) {
}
});
or as a query string parameter if you are using GET:
或作为查询字符串参数,如果您使用的是 GET:
$.ajax({
url: '@Url.Action("Action", "Controller")',
type: 'GET',
data: { id: rowid },
success: function(result) {
}
});
All those suppose that your controller action takes an id parameter of course:
所有这些都假设您的控制器操作当然采用 id 参数:
public ActionResult Action(string id)
{
...
}
So as you can see many ways to achieve the same goal.
因此,您可以看到实现同一目标的多种方法。