tags:

views:

185

answers:

3

I have a number of create and delete partial views that I want to reuse by calling from other views. The issue is that this requires me to pass the return route and routeValues to the create and delete controller methods so that they can generate an appropriate redirect back to the original view on success. I created some extensions and helpers to keep this tidy but it seems convoluted to approach the problem this way. Have I missed something? Is there a simple way to RedirectToAction when the (redirect) controller, action and routeValues can vary?

Example for clarity: both the Product A-Z Index View and the Product SomeCategory Index View have a delete button that calls the Delete View (which displays a "do you really wanna delete" message) which has a "Really Delete" button that posts back to the actual (POST) Delete method in the product controller. Once the product is deleted we need to return a RedirectToAction but since both the 'A-Z Index' and the 'SomeCategory Index' Views have a Delete link we have to dynamically set the action, controller and routeValues to whatever view called the delete initially.

This isn't difficult but it's extremely convoluted to pass the redirect values around all the controllers and views that handle the delete and it stands to reason there must be a saner way to do this.

A: 

did you consider using RedirectToRoute

RedirectToRoute(new {controller = "MyController", Action = "Create", id = ""});
Marwan Aouida
It wouldn't solve the problem which is maintaining the controller, action and routeValues of the calling view across the method calls.
grenade
A: 

Consider not using a whole view for the 'delete confirm'. Use a Html helper and a javascript 'confirm()'. ie. render the post form and delete link with a helper so that when the user clicks "delete" they get a js confirm prompting "sure to delete?" and on ok the function "returns true" and invokes the submit on the form to delete. then the delete action simply redirects to wherever it normally would. i'd hope you are using different delete actions for the different objects you are trying to delete. if your plan is to have a generic delete action, well that's harder (and not recommended IMO).

My delete helper includes lots of things, but the delete part looks like this (with 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 it (the 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 the Delete link with all the params for the route and ID etc. The js simply does the 'confirm' part then submits the tiny you can see is created by the helper.

[sorry if the samples are not 100% complete - i've had to remove lots of things: eg the helper and attached js have many different modes so as to support ajax POSTs etc]

cottsak
Your solution is valid for many situations but in our scenario, deletions require a view as there is a complex dependency tree below the item being deleted and this is displayed on the confirmation view. We also need dynamic redirection support on Create and Edit views that may be called by any number of other views that require a mechanism for returning to the calling view.
grenade
+1  A: 

Interrogate the Request.UrlReferrer in the Delete action ( the one that displays the confirmation view ) and store the referrer details in the temp data.

In the delete action, read the referrer details back out of the temp data and use the Redirect( string ) overload to redirect to the url that referred the user to the original delete request.

Neal