How can I temp go to another view to create entities
I am new to asp.net MVC and have to decide how I can implement the following. Can anybody please point me in the right direction
I have a controller which serves a view to create customers which all works fine
I have another controller which serves a vew to create invoices. But to create invoice I will need to search for customers which is all working fine. If I cant find a customer I will need to serve the create customer view, create the customer and then return to my invoice creation.
So my question is how from my Invoice controller i c开发者_C百科an get the customer controller to create a customer, get that customer details back and plug the details into my invoice view.
These controllers/views live in different areas
Remember that controllers serve specific requests (get a customer, view a customer, create an invoice).
In your case, you dont need to "get the customer controller to create a customer".
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.
You dont want to be moving back and forth from controllers - this goes against the principle of MVC.
It should be
URL Request -> Controller -> Model -> Controller -> View
.
not
URL Request -> Controller -> Controller -> Model -> Controller -> View.
Here's what i mean.
Customers Controller:
public ActionResult Index(int customerId)
{
var customer = Model.GetCustomerById(customerId);
return View(customer);
}
Invoices Controller:
public ActionResult GetInvoice(int customerId)
{
var customer = Model.GetCustomerById(customerId);
return View("Invoice", customer);
}
Model:
public Customer GetCustomerById(int customerId)
{
var customer = yourDatabaseRepository.GetCustomerById(customerId);
}
Know what i mean? Key is dont put the "GetCustomer" logic in the controller - abstract it away into the Model - accessible to all controllers.
Before I answer your technical question, think about the flow of the invoice page. I imagine you want to have sort of search box for the customer. If the customer isn't found you are asking to automatically redirect to a different controller, create the customer, and return back to the invoice page. You may be better served to alert the user that the customer wasn't found, and present them with the option to create the customer, (they could have miss-spelled the name, etc). Otherwise you'll end up with duplicate customers floating around with different invoices attached to each.
You can then redirect the user with the following (notice I included a return url & customer name), your customer controller can accept these parameters and pre-populate the name field as well as redirect back:
RedirectToAction("Create", "Customer", new { returnUrl = "YourUrl", NewCustomerName = "NewCustNameHere" }
If you do want to automatically do this I would do this in a method as RPM suggested.
精彩评论