我有一个功能CreatePerson(int id)
,我想从id
传递@Url.Action
。
以下是参考代码:
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person") + id";
上述代码无法将id
值传递给CreatePerson
函数。
答案 0 :(得分:103)
你可以这样传递:
Url.Action("CreatePerson", "Person", new RouteValueDictionary(new { id = id }));
OR也可以通过这种方式
Url.Action("CreatePerson", "Person", new { id = id });
答案 1 :(得分:21)
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));
答案 2 :(得分:3)
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})';
答案 3 :(得分: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"] })'" />
答案 4 :(得分:1)
尝试一下:
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person")?Id='+Id+'";
它可以正常传递参数。
答案 5 :(得分:0)
你应该以这种方式传递它:
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person",new { @id = 1});
答案 6 :(得分:0)
@Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme)
答案 7 :(得分: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!");
}
});
答案 8 :(得分:0)
试试这个
public ActionResult CreatePerson(string Enc)
window.location = '@Url.Action("CreatePerson", "Person", new { Enc = "id", actionType = "Disable" })'.replace("id", id).replace("&", "&");
你将在Enc字符串中获得id。
答案 9 :(得分:0)
需要两个或多个参数将Throw视图传递给控制器使用此语法... 试试。。
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;