2016-06-07 52 views
0

我正在開發一個ASP.Net MVC 5項目。我提交內容時需要重定向頁面。我用這個Javascript代碼:JavaScript重定向到控制器動作asp.net mvc

return Content("<script language='javascript' type='text/javascript'>alert('Edited...');window.location.href = 'ShowColor';</script>"); 

當我編輯

http://localhost:56583/Admin/EditColor/27

東西,我想重定向我的網頁

http://localhost:56583/Admin/ShowColor

用它去

上面的代碼

http://localhost:56583/Admin/EditColor/ShowColor

而且我用下面的代碼根據Redirecting to action from javascript,但它並沒有太多的工作:

return Content("<script language='javascript' type='text/javascript'>alert('Edited...');window.location.href = '@Url.Action('ShowColor', 'Admin')';</script>"); 

我不知道如何解決它。感謝您的任何幫助

+0

爲什麼不只是'返回RedirectToAction(「ShowColor」,「管理員」);'? –

+0

因爲我也想顯示提醒@StephenMuecke –

回答

1

我相信這是由於相對路徑。像下面的東西應該使它相對於域。

window.location.href='/Admin/ShowColor' 
0

要顯示警報和重定向,你需要調用你的控制器操作的錨標記,則控制器會看起來像:

public ActionResult AlertAndRedirect() 
    { 
     return Content("<script language='javascript' type='text/javascript'>alert('Good work, click to redirect.');</script>"); 
    } 

這將彈出一個對話框,然後重定向你。

相關問題