[英]how to pass parameter from @Url.Action to controller function
我有一個函數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));
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})';
這種將值從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"] })'" />
嘗試這個:
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person")?Id='+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(string Enc)
window.location = '@Url.Action("CreatePerson", "Person", new { Enc = "id", actionType = "Disable" })'.replace("id", id).replace("&", "&");
您將在Enc字符串中獲取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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.