3

Let's say I have an OrderController which handles orders. The user adds products to it through the view, and then the final price gets calculated through an AJAX call to a controller action.

The price calculation logic is implemented in a seperate class and used in a controller action. What happens is that I have many views from different controllers that need to use that particular action. I'd like to have some kind of a PriceController that I could call an action on. But then the view would have to know about that PriceController and call an action on it.

Is it bad practice for a view to call an action on a different controller from which it was rendered?

1
  • It depends on the location and purpose of the 2nd controller. If it returns e.g. a JsonResponse with the calculated amounts then it's fine. The calculations should live in a Service class that this and any other part of your system can call on, whether it's another Service, Controller, etc. So the AJAX call can call the API controller, pass in values, API controller calls ServiceCalculator and gets the calculated values, then passes back to API controller which turns it into a JsonResponse and back to AJAX Commented Aug 20, 2023 at 20:50

1 Answer 1

7

This seems like a pretty reasonable solution to me.

A view that has to request information should, and can, be able to request that information from any appropriate controller - I don't think there needs to be a 1:1 mapping if that's what your implying there should be?

2
  • Yes that's what I am implying. Commented Oct 24, 2013 at 14:51
  • 1
    @marco-fiset: How would I get customer details from an order view? Would I have to add GetCustomer actions on all controllers that could potentially need to provide a link to the customer details? Yikes... very wet violation of DRY. Customer details should be taken care of in a single place. Hmm. Sounds like a customer controller with a customer view. And calling it from wherever one would like to be able to get the details of the customer. Commented Oct 24, 2013 at 18:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.