如何将参数从@ Url.Action传递给asp.net mvc3中的控制器函数

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

以下是参考代码:

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

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

你可以这样传递它:

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

或者也可以通过这种方式

 Url.Action("CreatePerson", "Person", new { id = id }); 
 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)); 

通过这种方式将值从Controller传递到View:

 ViewData["ID"] = _obj.ID; 

以下是将View从View传回Controller的方法:

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

你应该如何通过它:

 public ActionResult CreatePerson(int id) //controller window.location.href = "@Url.Action("CreatePerson", "Person",new { @id = 1}); 
 @Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme) 

如果你在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!"); } }); 
 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})';