Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need to have too many things going on in a controller.<br> Think of a controller as a class that determines what the user wants and what it should see in the end.</p> <p>What user wants is determined by the ASP.NET MVC engine automatically for you, with the help of the routes you defined in the Global.asax file. Then the controller should decide what view the user should see as a result of the request he/she made.</p> <p>So for your application things would go something like this :</p> <p>Global.asax</p> <pre><code>routes.MapRoute("Cars", "cars/{id}/{action}", new { controller = "Cars", action = "Details", id = "" } ); //This route should give you the URLs you listed on your question </code></pre> <p>CarsController.cs</p> <pre><code>public class CarsController : Controller { //this action will be called for URLs like "/cars/423" //or /cars/423/details public ActionResult Details(int id) { //You would most likely have a service or business logic layer //which handles talking to your DAL and returning the objects //that are needed for presentation and likes Car car = SomeClass.GetCar(id); if (car == null) { //if there's no car with the ID specified in the URL //then show a view explaining that return View("CarNotFound"); } //if there's a car with that ID then return a view that //takes Car as its model and shows details about it return View(car); } //this action will be called for URLs like /cars/423/edit [AcceptVerbs(HttpVerbs.Get)] public ActionResult Edit(int id) { //again get the car, show an edit form if it's available for editing } //this action will be called for URLs like /cars/423/edit too //but only when the request verb is POST [AcceptVerbs(HttpVerbs.Post), ActionName("Edit")] public ActionResult Save(int id) { //save the changes on the car, again using your business logic layer //you don't need the actual implementation of saving the car here //then either show a confirmation message with a view, //saying the car was saved or redirect the user to the //Details action with the id of the car they just edited } } </code></pre> <p>So basically this is how you would set up your controller(s) for an application like that.</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