如何从@Url.Action传递参数到控制器函数

77

我有一个函数 CreatePerson(int id),我想从 @Url.Action 传递 id

以下是参考代码:

public ActionResult CreatePerson(int id) //controller 
window.location.href = "@Url.Action("CreatePerson", "Person") + id";

上述代码未能将id值传递给CreatePerson函数。

10个回答

127

你可以这样传递它:

Url.Action("CreatePerson", "Person", new RouteValueDictionary(new { id = id }));

OR也可以这样传递参数

Url.Action("CreatePerson", "Person", new { id = id });

10
如果代码在JavaScript中,会怎样呢? window.refresh({url: '@Url.Action("CreatePerson", "Person", new { id = Id })', --> 这段代码无法正常工作,因为我无法将变量传递进去。只有硬编码的情况下才能正常工作:@Url.Action("CreatePerson", "Person", new { id = "someId" })', --> 如何让它正常工作呢? - chiapa
我也在寻找这个答案。 - Pedro Franco
1
如果一个函数在 JavaScript 文件中,你可以将其 URL 放在视图的隐藏字段中,然后使用 JavaScript 调用它。例如:<input id="url">@Url.Action("CreatePerson", "Person", new { id = Id })<\input>然后在 JavaScript 中调用它:$("#url").val(); - I_Khanage
7
这是我的操作方法:var url = '@Url.Action("Foo","Bar", new {id="replaceToken"})'; 然后url = url.replace("replaceToken", yourID); - James
1
@chiapa 我知道我在回复一个很久以前的评论,但是无论如何,将它们作为单独的参数传递比作为整个Razor命令更容易,例如 new { action = "CreatePerson", controller = "Person", id = Id },然后在你的Action中使用RedirectToAction(action, controller, id)重定向到接收到的参数动作。 - Tiramonium

28
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));

5
public ActionResult CreatePerson(int id) //controller 

window.location.href = '@Url.Action("CreatePerson", "Person")?id=' + id;

或者

var id = 'some value';
window.location.href = '@Url.Action("CreatePerson", "Person", new {id = id})';

4

试试这个:

public ActionResult CreatePerson(int id) //controller 

window.location.href = "@Url.Action("CreatePerson", "Person")?Id='+Id+'";

参数传递正常,工作良好。


3
需要通过视图向控制器传递两个或更多参数,请使用以下语法... 尝试一下。
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(/&amp;/g, "&");
window.location.href = Url;

2
这是从控制器传递值到视图的方法:

ViewData["ID"] = _obj.ID;

以下是从视图传递值回控制器的方法:

<input type="button" title="Next" value="Next Step" onclick="location.href='@Url.Action("CreatePerson", "Person", new { ID = ViewData["ID"] })'" />

1

试试这个

public ActionResult CreatePerson(string Enc)

 window.location = '@Url.Action("CreatePerson", "Person", new { Enc = "id", actionType = "Disable" })'.replace("id", id).replace("&amp;", "&");

你将会在 Enc 字符串内获得该 ID。


谢谢,它正在工作中.... .replace("id", encodeURIComponent(id)) 这也是必需的..... - Gupta

0

你应该这样传递它:

public ActionResult CreatePerson(int id) //controller 
window.location.href = "@Url.Action("CreatePerson", "Person",new { @id = 1});

0
@Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme)

0
如果你正在JavaScript中使用Url.Action,那么你可以:
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!");
  }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接