shima amini shima amini - 6 months ago 88
Javascript Question

javascript redirect to controller action asp.net mvc

I'm developing an ASP.Net MVC 5 project . I need to redirect page when I submit something. I used this Javascript code :

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


when I edit something in

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

I want redirect my page to

http://localhost:56583/Admin/ShowColor

With above code it goes to

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

Also I used below code according to Redirecting to action from javascript but it didn't work too :

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


I have no idea how fix it. thanks for any help

Answer

I believe this is due to relative paths. Something like the below should make it relative to the domain.

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