MVC5 Ajax PartialView and Back Button - ajax

I have been searching for hours but yet cannot find a solution. I can't imagine that I am the first to come across this.
I have an Ajax form that calls a method in my controller, that returns a PartialView:
public PartialViewResult InventoryActivityAjax(int divisionId)
{
// ajax call to get products
return PartialView("_Index", builder.BuildIndexAjaxView(divisionId));
}
In this view is a link to another page. A lot of users click these links, and then press the Back button, and the page that gets re-loaded is the original page without the detail from the Ajax call.
Does anyone know how I can get the last page loaded? I would rather not recall the Ajax call either as it takes a second or so to load.
TIA,
Mark

Related

How to load different views in one div that situated in home page on button click using ajax,jquery,codeigniter

I have a page like home now what I want is if I click a button or link like a menu a corresponding view must load in that div in the home page. On every button click the views are must change inside the div without refreshing the entire page.
I know this can be done with ajax and jquery.
on click event of button call a javascript method, collect information which affect different views, send ajax request in a controller with post data, according to data, load a view like
$res = $this->load->view('myview1')
Remember, ajax request should not be of "json" type.
Ajax request will send a string, take this string in a javascript var.
now in javascript
$('#div_id').html(res);

Can I cancel a controller request half way in ASP MVC?

Am very new to ASP MVC
I have a list of menu options that the user can choose. Each menu selection triggers one controller as per MVC architecture .
But unfortunately some of these controllers take some time for execution to make a visible change. Because the large data needs to be binded to a grid.So all that time browser hangs(shows loading).
The worst case occurs if user selects an option which could be easily loaded, after a time consuming menu option.
Can someone help on how to dismiss one controller action request if another comes in?
Right now its the summation of all user selected actions
Eg: If user selects Menu1 and after that Menu2 . And my Menu1 controller action is taking some time to execute. By that time User clicks Menu2 which is easily loaded if clicked for first time. But since Menu2 is clicked user has to wait a long.
This is a bit old but I will attempt to answer anyway since I ran into this problem recently. If you are using MVC 5, the other requests can be cancelled for you. The trick is to use async controller actions and to add a CancellationToken parameter.
public async Task<ActionResult> MyReallySlowReport(CancellationToken cancellationToken)
{
List<ReportItem> items;
using (ApplicationDbContext context = new ApplicationDbContext())
{
items = await context.ReportItems.ToListAsync(cancellationToken);
}
return View(items);
}
When the user navigates to another page, the request is cancelled. MVC will signal to the cancellation token that the request has been cancelled. As long as you also pass the cancellation token to the async data access method like I did above, the query will be cancelled and the server will stop working on the request immediately.
I have put together a full explanation here: http://www.davepaquette.com/archive/2015/07/19/cancelling-long-running-queries-in-asp-net-mvc-and-web-api.aspx

Ajax Load Div based on URI (Instead of onClick)

I am converting an old web application. In the legacy version, there is a page which displays some default content when the user navigates to it. There is a link within that page which reloads the page but with a querystring showform=yes which causes a form to be displayed instead of the content.
In the old version, the page was like this:
http://site.com/directory.asp - for the main page with the content
http://site.com/directory.asp?showform=yes - called when the user
clicked the button to show the form.
In the new version of the web app, that same page is set up with ajax, in that there is a div which contains the default content when someone visits http://site.com/directory. There is a button, which, when clicked (from within that page), some ajax is invoked which swaps out the content and displays the form. There is no change in the URI/querystring.
Here is my question - In the legacy version of the web app, there are some other pages which link the user directly to the page with the form (http://site.com/directory.asp?showform=yes) e.g. so they don't need to click the button once the arrive at directory.asp.
Is there a what I can mimimc this behavior based on how I have that page set up now e.g. displaying the form via ajax in a div?)
Initially I thought that perhaps ajax can be triggered based on the presence of a querystring or an anchor in the URI e.g. /directory#form but I am not sure if that is possible.
I would prefer a solution that is not dependent on jQuery, but will consider it if there are no other options.
Thanks in advance for looking, and please let me know if I can further clarify.
Thanks,
Gary
yes you can trigger AJAX call on the basis of a boolean variable which you can set through the query string.
The default value for the boolean variable is false which forces the user to click on the button to refresh the form section.
But when the comes back on the same page the query string will set the boolean variable to true whereby the javascript function containing the AJAX code is invoked from your JSP/ASP or HTML.

navigate back and display the previous page without reloading the previous page variables and designs?

I have one issue in my program, i have three pages in my program, Main page, first page and second page. I'm able to navigate from the main page to the first page and from the first page to the second page. I have a stack panel in the first page. It consists of children which are dynamically created from the db. This page contains a OnNavigateTo method, for bringing the elements from the main page and displaying it, And i wrote my codings in this method to add dynamically children to the stackpanel. when navigating back from the second page to the first page, the compiler will goes to the OnNavigateTo method and regenerate the children of the stackpanel once again.
My problem is:
i want to show the previously displayed first page with all the data, when the back button is pressed. I doesn't want the page to regenerate again. I want the static page which i have previously when i go back from the second page to the first page.
How to do it? please help me with some piece of code... Thank You..
Do the logic in the constructor, because it doesn't get called when navigating back (unless the app has been tombstoned). OnNavigatedTo gets called every time no matter how you navigate to the page.
Read this for more info:
WP7 Development Tip of the Day: Page Startup: The Constructor

Hide the Partial view and show another partial view in mvc3

I have a hyperlink column in the grid. On clicking the link i have to hide the partialview(grid section) and have to show/load another partialview which is the detail section. Please provide solution
You could use javascript. With jQuery that will correspond to the .toggle() function or the .show()/.hide() functions. So basically you will subscribe to the click event of the link and inside this handler show and hide the respective sections. For this to work you should obviously place those partials inside placeholder divs so that you could show/hide the entire placeholder.
If in addition to showing the partial you need to fetch some fresh information from the server then you could use AJAX to request a controller action that will return the fresh data of the partial view that you will inject into the DOM at the correct placeholder location. In order to send an AJAX request in jQuery you could use the $.ajax() function or directly the .load() function.

Resources