如何将参数从@ Url.Action传递到控制器功能


68

我有一个功能CreatePerson(int id),我想通过id@Url.Action

下面是参考代码:

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

上面的代码无法将id值传递给CreatePerson函数。

Answers:


117

您可以这样发送:

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

或者也可以通过这种方式

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

8
如果代码在javascript中怎么办?window.refresh({url: '@Url.Action("CreatePerson", "Person", new { id = Id })',->此代码中断,因为我无法在其中传递变量。它只有在硬编码的情况下才有效:@Url.Action("CreatePerson", "Person", new { id = "someId" })',->如何使其工作?
chiapa 2015年

我也在寻找这个答案
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);
詹姆斯,

@chiapa我知道我要回复一个漫长的死评论,但是无论如何,将它们作为参数而不是整个Razor命令分别传递会更容易,如中所述new { action = "CreatePerson", controller = "Person", id = Id },然后在您的Action中重定向到使用RedirectToAction(action, controller, id)
Tiramonium

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

4
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+'";

它正在正常传递参数。


2

这种将值从Controller传递到View的方法:

ViewData["ID"] = _obj.ID;

这是将值从View传递回Controller的方法:

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

2

需要两个或多个参数将Throw视图传递给控制器​​使用此语法... 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(/&amp;/g, "&");
window.location.href = Url;

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))这也Reqired .....
古普塔

0

您是否应该以这种方式通过它:

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


0

如果您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 using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.