Share via


Redirect to another page using JavaScript + Razor

Question

Saturday, March 10, 2012 10:58 AM

Hello, I just want to ask for some help about this. Well, if i have a directory like this:

-root

-root/baz.cshtml

-root/foo

-root/foo/bar.cshtml

So I have the bar file inside the foo folder. Now I want to redirect to page using this code when a link was clicked. I am using jQuery as the framework for JS.

A snippet from bar.cshtml

document.location = 'Link to root folder' + '/baz/' + @UrlData[0];

All replies (1)

Saturday, March 10, 2012 7:25 PM âś…Answered

I am not sure I understand your answer.

If your problem is to pass the redirection url to javascript, a possible solution is

in c# code:

@{
    var root = Request.ApplicationPath;
    var par = UrlData[0];
}

and in javascript

<script type="text/javascript">
  ....
  var root = '@root';
  var par = '@par';
  document.location = root + 'baz/' + par;
  ....
</script>

Hope can help.