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