views:

38

answers:

1

Is there any way to change the url after an ajax call. I have a page (eg., url - xyz.com/employees) which displays all the employees, and there is a link on the page named 'Add Employee' which returns a partial view. As this being an ajax call, the url remains the same, is there anyway to change the url to xyz.com/addemployee after returning the partial view??

+1  A: 

Many AJAX applications, such as gmail, use the hashtag (#) part of the url for this purpose. You can change the url using javascript using document.location. As long as the part of the url that is before the hashtag remains the same, the page will not refresh. You will then need to build a javascript function that reads the hashtag from the url and opens the appropriate content. For example.

http://www.example.com/Controller/Action?param=something

And when they click on the link to add employee.

http://www.example.com/Controller/Action?param=something#add-employee

Next you would need to add a function that get called when to page is loaded to read the hashtag and open the appropriate partial page.

mattweg
thanks matt for the quick response! actually I have added a custom route 'addemployee', this route points to 'addemployee' action in my controller. This action checks whether the request is an ajax request, if so, it returns the partial view; else, the addemployee view. By this the user can directly type xyz.com/addemployee or go to default page (which displays all the employees) and the click 'add employee' link. Just to be consistent, I want the users to see xyz.com/addemployee always, no matter it is an ajax call or not.
Gokulnath
As far as I know, most browsers are going to refresh the page if the url changes by anything more than the method I described above.
mattweg