Can I cancel a controller request half way in ASP MVC? - asp.net-mvc-3

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

Related

Identify which button in message was clicked

I'm sending messages with Block elements(buttons) to users using Slack API (python-slackclient). Different users can get number of these and can click the buttons with a delay obviously, for example the next day.
I need to identify which button in which message was clicked.
After checking the documentation on Slack API pages it looks like action_id is the way to go as I can specify it in my request and assign it a unique value. I shall get the action_id back in response coming to my endpoint once user clicks one of the buttons which will allow me to match it with sent message.
Is this the correct way to achieve it? Are you aware any better way to implement this?
You can use an action_id on your button in order to tie your button to a response action.
So if you have several buttons that should all trigger the same action, but you'd still need to know which of those buttons was clicked, maybe you could give them all the same action_id (so you can link them to the same action), but specify unique button values.
You need to make sure your action_ids are unique, BUT you can receive a message or an action using RegEx and thus still point them all to the same handler.
For example, this action handler receives all button presses where the action_id starts with "hello"
app.action(/^hello.*/, async ({ body, ack, say }: any) => {
await ack();
await say(`<#${body.user.id}> clicked the button`);
});

NGXS ofActionSuccessful

I have an angular component that subscribes to ofActionSuccessful. This action is dispatched as the user clicks on a button on the page. It is possible that the user may click on this button multiple times resulting in multiple dispatch events for this action
this.actions$.pipe(ofActionSuccessful(UpdateCommunicationPreferences)).subscribe(() => {
console.log('Action Successful');
});
this.store.dispatch(
new UpdateCommunicationPreferences(this.communicationPreferenceForm.value)
);
The issue that I am running into is ofSuccessful is triggered in an accumulated manner. In other words, the first time the user clicks on the button, the "Action Successful" is printed once. Second time the user clicks on the button, it is printed twice and so forth.
Can anyone help if there is a way to not get duplicate ofSuccessful events for the past actions?
Appreciate any insight.
This package was developed with your issue in mind:
https://www.npmjs.com/package/#ngxs-labs/actions-executing
#Select(actionsExecuting([UpdateCommunicationPreference])) myActionIsExecuting$: Observable;
you can use myActionIsExecuting$ in your template with async pipe. you can also simply subscribe to myActionIsExecuting to familiarize yourself with what is returned. null will be returned when all instances of that particular action are finished.
I hope that helps!
Take a look this: https://www.ngxs.io/advanced/cancellation
Extracted from the link: "If you have an async action, you may want to cancel a previous Observable if the action has been dispatched again. This is useful for canceling previous requests like in a typeahead."
Alternatively, you can consider a pattern that disables the UI when the button is clicked (e.g. via 'in progress' dialog) before triggering the action dispatch. When the action is done processing, close the dialog.
Hope this helps.

MVC5 Ajax PartialView and Back Button

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

APEX: Call JavaScript function after validation but before processing

I have been tasked with re-creating an old PL/SQL Web Toolkit front end in Application Express (Apex).
I would like to display a popup after the page is submitted and after computations and validations.
The page can be submitted by clicking a button OR by hitting enter.
I have googled and got a modal popup working (called by javascript) but I can't seem to call it at the right point. I can call it on submit (before the validations etc.) but not after the validations.
I have tried creating a branch to URL at the correct processing point and then have the URL set to:
javascript:openForm();
But then I get a page will not display error.
Can anyone out there explain how I could do this?
Apex Version: 4.0.2
Oracle 10g
I suppose what you want to do is to perform the validations, have values submitted to session state, but not execute further processes.
However, when you submit the page it is sent to the server; and everything you see in the page processing region will sequentially fire. There is no way to halfway through the processes call a javascript function, since basically you are not on the clientside anymore.
What you can do is to create a branch after your validations to the same page. Redirect to it but provide a REQUEST value, for example OPENFORM.
Create a dynamic action, firing on page load, with a true action that executes javascript and opens up your modal page. Then set the condition on your dynamic action to Request = Expression 1, providing the request value to Expression 1 (OPENFORM).
(Note that this is the Conditions region, and not the 'Condition' field of the 'When' region)
This should cause the page to be submitted, validated, then re-loaded but with a request value, and the dynamic action firing, opening your modal page.
Just a point of interest:
If you have actual processes on this page though, then be careful with the Enter key. Buttons by default submit to session with the request value set to their name, and thus making it possible to conditionally execute processes or branches. The enter key does not submit with a request value set i believe. So if your branch is conditional, the enter key might simply skip over it.

Ways to monitor the ajax call and wait for the same

I am calling a Ajax call from my asp.net page. I have a combo box called technology and when i select some value from it, i am calling an Ajax call which will trigger a function which talks to DB and then returns the Vendor name. Here if the data is very less then then call immediately returns and updates the associated fields. If the data is huge, the Ajax call is taking long time and before that if the user saves the form, it stores with null value. Here i should restrict the user from clicking any thing on the screen till the Ajax call is finished. For that, i need to know how can we track the Ajax call completion and how the ajax call can be monitored?
Can somebody help me in this?
Thanks in advance,
Ramprasad
I suggest you use Fiddler, its excellent for things like this.
I suggest you just to block form send button till all data would be filled. So user will need to wait of loading this select box. And also change value of combo box to "loading.." or something like this, so user see that there are some things doing.
Also you shouldn't block all from when combo box loading, because at this time user can fill some other form fields.

Resources