我有一个函数 CreatePerson(int id)
,我想从 @Url.Action
传递 id
。
以下是参考代码:
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person") + id";
上述代码未能将id
值传递给CreatePerson
函数。
我有一个函数 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 }));
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+'";
参数传递正常,工作良好。
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;
ViewData["ID"] = _obj.ID;
以下是从视图传递值回控制器的方法:
<input type="button" title="Next" value="Next Step" onclick="location.href='@Url.Action("CreatePerson", "Person", new { ID = ViewData["ID"] })'" />
试试这个
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("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme)
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!");
}
});
window.refresh({url: '@Url.Action("CreatePerson", "Person", new { id = Id })',
--> 这段代码无法正常工作,因为我无法将变量传递进去。只有硬编码的情况下才能正常工作:@Url.Action("CreatePerson", "Person", new { id = "someId" })',
--> 如何让它正常工作呢? - chiapanew { action = "CreatePerson", controller = "Person", id = Id }
,然后在你的Action中使用RedirectToAction(action, controller, id)
重定向到接收到的参数动作。 - Tiramonium