C# 如何将参数从@Url.Action 传递给控制器函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13017513/
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
how to pass parameter from @Url.Action to controller function
提问by user1120572
I have a function CreatePerson(int id), I want to pass idfrom @Url.Action.
我有一个函数CreatePerson(int id),我想id从@Url.Action.
Below is the reference code:
下面是参考代码:
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person") + id";
the above code fails to pass idvalue to CreatePersonfunction.
上面的代码无法将id值传递给CreatePerson函数。
回答by HatSoft
you can pass it this way :
你可以这样传递:
Url.Action("CreatePerson", "Person", new RouteValueDictionary(new { id = id }));
OR can also pass this way
OR也可以这样通过
Url.Action("CreatePerson", "Person", new { id = id });
回答by Zeyad Qunees
should you pass it in this way :
你应该以这种方式通过它吗:
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person",new { @id = 1});
回答by Anjan Kant
This way to pass value from Controller to View:
这种将值从控制器传递到视图的方法:
ViewData["ID"] = _obj.ID;
Here is the way to pass value from View to Controller back:
这是将值从 View 传递回 Controller 的方法:
<input type="button" title="Next" value="Next Step" onclick="location.href='@Url.Action("CreatePerson", "Person", new { ID = ViewData["ID"] })'" />
回答by Pàldi Gerg?
public ActionResult CreatePerson (string id)
window.location.href = '@Url.Action("CreatePerson", "Person" , new {id = "ID"})'.replace("ID",id);
public ActionResult CreatePerson (int id)
window.location.href = '@Url.Action("CreatePerson", "Person" , new {id = "ID"})'.replace("ID", parseInt(id));
回答by Sanjay Sharma
@Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme)
回答by Jain
public ActionResult CreatePerson(int id) //controller
window.location.href = '@Url.Action("CreatePerson", "Person")?id=' + id;
Or
或者
var id = 'some value';
window.location.href = '@Url.Action("CreatePerson", "Person", new {id = id})';
回答by Keerthi
If you are using Url.Actioninside JavaScript then you can
如果您Url.Action在 JavaScript中使用,那么您可以
var personId="someId";
$.ajax({
type: 'POST',
url: '@Url.Action("CreatePerson", "Person")',
dataType: 'html',
data: ({
//insert your parameters to pass to controller
id: personId
}),
success: function() {
alert("Successfully posted!");
}
});
回答by V.V
Try this
尝试这个
public ActionResult CreatePerson(string Enc)
window.location = '@Url.Action("CreatePerson", "Person", new { Enc = "id", actionType = "Disable" })'.replace("id", id).replace("&", "&");
you will get the id inside the Enc string.
您将在 Enc 字符串中获得 id。
回答by Benet Shibin
Try this:
尝试这个:
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person")?Id='+Id+'";
it's working fine passing parameter.
它工作正常,传递参数。
回答by Gupta
Need To Two Or More Parameters Passing Throw view To Controller Use This Syntax... Try.. It.
需要两个或更多参数将抛出视图传递给控制器使用此语法......试试......它。
var id=0,Num=254;var str='Sample';
var Url = '@Url.Action("ViewNameAtController", "Controller", new RouteValueDictionary(new { id= "id", Num= "Num", Str= "str" }))'.replace("id", encodeURIComponent(id));
Url = Url.replace("Num", encodeURIComponent(Num));
Url = Url.replace("Str", encodeURIComponent(str));
Url = Url.replace(/&/g, "&");
window.location.href = Url;

