Suggestions Required for Processing Dynamic Forwarding Routes in ASP.Net MVC

I have several options for creating and deleting partial views that I want to reuse by calling from other views. The problem is that for this I need to pass the return route and routeValues ​​to the methods for creating and removing the controller so that they can generate a suitable redirection to the original idea of ​​success. I have created several extensions and helpers to maintain this neatness, but it seems to be confusing to solve this problem. Did I miss something? Is there an easy way for RedirectToAction when the controller (redirection), action and routeValues ​​can change?

An example for clarity: both in the AZ product index view and in the product index index in some categories there is a delete button that causes the message "Delete" (which displays the message "do you really want to delete"), in which there is a button "Really delete" returns to the actual POST removal method in the product controller. After deleting the product, we need to return RedirectToAction, but since both the AZ Index and SomeCategory Index have a Delete link, we must dynamically set the action, controller and routeValues ​​for any view called delete first.

It's not complicated, but it is extremely confusing to pass the redirect values ​​in all the controllers and views that handle the deletion, and this is reasonable, so there should be a more robust way to do this.

+1
source share
3 answers

Interrogate Request.UrlReferrer in the Delete action (the one that displays the confirmation window), and save the referrer data in temp data.

In the delete action, read the referrer data back from the temporary data and use the redirect overload (string) to redirect to the URL that passed the initial delete request to the user.

+1
source

Do you consider using RedirectToRoute

RedirectToRoute(new {controller = "MyController", Action = "Create", id = ""}); 
0
source

Think not to use the entire view to confirm the deletion. Use helper and javascript "confirm ()". i.e. visualize the mail form and delete the link with an assistant, so that when the user clicks "delete" they get a confirmation hint js "do you have to delete it?". and on ok, the function "returns true" and causes the form to be submitted for deletion. then the delete action is simply redirected to where it would normally be. I hope you use different deletion actions for different objects that you are trying to delete. if your plan is to have a general delete action, so this is more complicated (and IMO is not recommended).

My removal assistant includes many things, but the removal part looks like this (using snips):

  string deleteLink = String.Format(@"<a onclick=""deleteRecord({0})"" href='#'>Delete</a><form id='deleteForm' method='post' action='" + routeRelativePath + "/" + actionPrefix + "Delete/" + model.ID + @"'></form>", model.ID); 

.. and he (helper) attaches some js too:

  function deleteRecord(recordId) { if(confirm('Are you sure you want to delete this {friendlyModelName}?\nNOTE: There is no Undo.')) { // Perform delete var action = "{routeRelativePath}/{actionPrefix}Delete/" + recordId; // jQuery non-AJAX POST version $("form#deleteForm").submit(); } } 

.. you can see that the helper creates a Delete link with all the parameters for the route and identifier, etc. Js just performs the “confirm” function and then sends the tiny element you see created by the helper.

[sorry if the samples are not 100% full - I had to delete a lot of things: for example, helper and attached js have many different modes to support ajax POST, etc.]

0
source

All Articles