r.r*_*r.r 38 javascript c# asp.net-mvc redirect
我有一个输入类型="按钮"
<input type="button" name="DeleteJob" runat="server" value="Löschen" onclick="DeleteJob()" />
Run Code Online (Sandbox Code Playgroud)
和JavaScript方法:
function DeleteJob() {
if (confirm("Do you really want to delete selected job/s?"))
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能返回true,重定向到Action DeleteJob?
[HttpGet]
public ActionResult DeleteJob(string selectedObject)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
Pet*_*rfy 36
重定向:
function DeleteJob() {
if (confirm("Do you really want to delete selected job/s?"))
window.location.href = "your/url";
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
yoj*_*o87 32
function DeleteJob() {
if (confirm("Do you really want to delete selected job/s?"))
window.location.href = "/{controller}/{action}/{params}";
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
RJB*_*RJB 14
我希望我能评论yojimbo87的回答来发布这个,但我还没有足够的声誉来发表评论.有人指出,这种相对路径只能从根本起作用:
window.location.href = "/{controller}/{action}/{params}";
Run Code Online (Sandbox Code Playgroud)
只是想确认您可以使用@ Url.Content来提供绝对路径:
function DeleteJob() {
if (confirm("Do you really want to delete selected job/s?"))
window.location.href = '@Url.Content("~/{controller}/{action}/{params}")';
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
was*_*tim 11
也许更好用DeleteJob url而不是按钮制作锚?
<a href="<%=Url.Action("DeleteJob", "YourController", new {selectedObject="someObject"})%>" onclick="return DeleteJob()">Löschen</a>
Run Code Online (Sandbox Code Playgroud)
并使用你已经写过的javascript:
function DeleteJob() {
if (confirm("Do you really want to delete selected job/s?"))
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果函数返回true,您将被重定向.如果函数返回false,您仍然会停留在页面上.
使用@ Url.Action方法.无论您部署到哪个IIS服务器,这都将起作用并确定正确的路由.
Example- window.location.href ="@ Url.Action("Action","Controller")";
所以在Home控制器上的Index操作的情况下 - window.location.href ="@ Url.Action("Index","Home")";
归档时间: |
|
查看次数: |
128809 次 |
最近记录: |