Hello. I'm very new to mvc. I need to figure out how to redirect a user to another action from within a View, but I'm not sure how. Normally, I would do the redirect within my controller but for this particular senario, it needs to be within the View.
There are a few ways to redirect from a view. Are you responding to a button click or simply redirecting when the view loads? If you are responding to a button click, then you can do it via JQuery.
For example you can respond to a button click like this:
Goalie35
Member
194 Points
192 Posts
MVC Razor: Redirect to action within a view?
May 17, 2012 02:56 PM|LINK
Hello. I'm very new to mvc. I need to figure out how to redirect a user to another action from within a View, but I'm not sure how. Normally, I would do the redirect within my controller but for this particular senario, it needs to be within the View.
I've tried code such as
@RedirectToAction("Index", "Home")...but had no luck. Also tried
@Url.Action("Index", "Home")...but that didn't work either.
How can I redirect from within this View?
Thanks
Junior_Pache...
Member
20 Points
5 Posts
Re: MVC Razor: Redirect to action within a view?
May 17, 2012 04:31 PM|LINK
I try to do the following: Declare "Url.Action @ (" Index "," Home ")" within the "@ Html.Hidden" and call via jQuery.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: MVC Razor: Redirect to action within a view?
May 17, 2012 04:44 PM|LINK
There are a few ways to redirect from a view. Are you responding to a button click or simply redirecting when the view loads? If you are responding to a button click, then you can do it via JQuery.
For example you can respond to a button click like this:
<input type="button" id="btnRedirect" value="Redirect"/> <script type="text/javascript"> $(function(){ $("#btnRedirect").click(function(){ window.location.href = "@Url.Action("ActionName","Controller")"; }); }); </script>RedirectToAction is something that you should only use in the controller, it's not for use in the view.
Can you give some more details about what you are trying to do and your scenario so that we can better help you solve this problem? Thanks!
Blog | Twitter : @Hattan
HJSh
Member
4 Points
3 Posts
Re: MVC Razor: Redirect to action within a view?
Dec 18, 2012 11:01 AM|LINK
i think using javascript is dirty so my solution would be but if you don't need user interaction:
Response.Redirect(Url.Action("Details", new { id = Model.ID }));