Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Remember that controllers serve specific requests (get a customer, view a customer, create an invoice).</p> <p>In your case, you dont need to "get the customer controller to create a customer".</p> <p>Just create a regular method which is called "GetCustomer" for example. This can be called from multiple controllers. This method could live anywhere - perhaps in a helpers assembly in your model namespace (assuming you have your Model in a seperate class library, which is what i do). Just call this from your Invoice controller.</p> <p>You dont want to be moving back and forth from controllers - this goes against the principle of MVC.</p> <p>It should be </p> <p><code>URL Request -&gt; Controller -&gt; Model -&gt; Controller -&gt; View</code>.</p> <p>not </p> <p><code>URL Request -&gt; Controller -&gt; Controller -&gt; Model -&gt; Controller -&gt; View.</code></p> <p>Here's what i mean.</p> <p><strong>Customers Controller:</strong></p> <pre><code>public ActionResult Index(int customerId) { var customer = Model.GetCustomerById(customerId); return View(customer); } </code></pre> <p><strong>Invoices Controller:</strong></p> <pre><code>public ActionResult GetInvoice(int customerId) { var customer = Model.GetCustomerById(customerId); return View("Invoice", customer); } </code></pre> <p><strong>Model:</strong></p> <pre><code>public Customer GetCustomerById(int customerId) { var customer = yourDatabaseRepository.GetCustomerById(customerId); } </code></pre> <p>Know what i mean? Key is dont put the "GetCustomer" logic in the controller - abstract it away into the Model - accessible to all controllers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload