How to prevent multiple messages in MVC3?

The user wants to publish on his blog, he fills in the field and clicks submit .

The site is slow, it clicks again, and again and again.

It was finally saved, but now it checks its messages and sees 4 messages.

How can I prevent this? If the user clicks to send once when I want to do nothing for the next clicks or abort previous , and start a new message, depending on what makes sense or is recommended.

In shape with

 @using (Ajax.BeginForm(...)) { } 
+5
source share
4 answers

The easiest way to achieve this is to disable the submit buttons after clicking.

Add the following javascript (jQuery) to your view:

 $(function () { $('input[type="submit"]').click(function() { $(this).attr('disabled', 'disabled'); }); }); 

Remember to enable the buttons after completing the ajax request, if necessary:

 $('input[type="submit"]').removeAttr('disabled'); 

UPDATE:

It is better to disable the submit button in the event handler of the form, because the user can submit the form by pressing the enter button:

 $(function () { $('form').submit(function() { $('input[type="submit"]', this).attr('disabled', 'disabled'); }); }); 
+17
source

There are many solutions.

One of them is to create a random GUID with a form as a hidden field that is inserted into the database with the message. Before inserting it, it checks GUID already exists.

Alternatively, you can use a real property, such as DateTime (which is sent on the client side as a hidden field) or "Message header".

+3
source

How about when the user clicks on the action link for the mail area in which you create a real post, but set its โ€œStatusโ€ to a draft ?. After you insert InsertOnSubmit (), the message immediately redirects to the action that you retrieve and edit. This happens so quickly that the user will not notice. Therefore, if the site is slow, and it clicks โ€œSendโ€ several times, you will not generate new records, just saving them in one record. I use this process in the task list application I'm working on. See code below.

Ticket controller for the best task list

This may not be the most bizarre solution, but it may lead you to a more creative solution that will work for you.

0
source

My vote for ActionFilters. This blog post contains code that simply enters your project and embellishes your actions.

0
source

All Articles