如何将参数从@ 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})';
- 将ASP.NET MVC Razor @helper函数转换为辅助类的方法
- 在另一个控制器中redirect到操作
- 简单的ASP.NET MVC CRUD视图在JavaScript UI对话框中打开/closures
- 在ASP.Net MVC应用程序中设置文化
- 如何创build文件并通过ASP.NET MVC中的FileResult返回?
- 如何使用JavaScript而不是提交button发布ASP.NET MVC Ajax表单
- 为什么Html.ActionLink呈现“?Length = 4”
- “区域”之间的ASP.NET MVC`Html.ActionLink`
- 如何从HttpPost创build操作方法中知道选中的checkbox?