我有一个功能CreatePerson(int id)
,我想通过id
从@Url.Action
。
下面是参考代码:
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person") + id";
上面的代码无法将id
值传递给CreatePerson
函数。
Answers:
您可以这样发送:
Url.Action("CreatePerson", "Person", new RouteValueDictionary(new { id = id }));
或者也可以通过这种方式
Url.Action("CreatePerson", "Person", new { id = id });
new { action = "CreatePerson", controller = "Person", id = Id }
,然后在您的Action中重定向到使用RedirectToAction(action, controller, 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));
需要两个或多个参数将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(/&/g, "&");
window.location.href = Url;
您是否应该以这种方式通过它:
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)
window.refresh({url: '@Url.Action("CreatePerson", "Person", new { id = Id })',
->此代码中断,因为我无法在其中传递变量。它只有在硬编码的情况下才有效:@Url.Action("CreatePerson", "Person", new { id = "someId" })',
->如何使其工作?