Carpool Project: Part #7

I am now moving into the site navigation of the carpool site. It was my 1st real foray into routing and after a couple of minutes, I realized it is straightforward. I had 2 controllers:

1 for the main page that shows 2 grids (current carpools and uncovered practices)

1 for the carpool update, insert, and delete pages

Here is an example of the Update method (so far):

1 public ActionResult Edit(int carpoolId) 2 { 3 Carpool.Domain.Carpool carpool = CarpoolFactory.GetCarpool(carpoolId); 4 return View(carpool); 5 } 6 7 [HttpPost] 8 public ActionResult Edit(int carpoolId, FormCollection collection) 9 { 10 try 11 { 12 Carpool.Domain.Carpool carpool = CarpoolFactory.GetCarpool(carpoolId); 13 UpdateModel(carpool); 14 CarpoolFactory.UpdateCarpool(carpool); 15 return RedirectToAction("Index"); 16 } 17 catch 18 { 19 return View(); 20 } 21 } 22  

 

I had to do some redirecting – there is no Index controller/page for the carpool as it is all handled out of Main.Index. I created the follwoign routes to allow me to redirect from one controller to the next:

1 routes.MapRoute( 2 "EditCarpool", 3 "Home/Edit", 4 new { controller = "Carpool", action = "Edit" } 5 ); 6 7 routes.MapRoute( 8 "CreateCarpool", 9 "Home/Create", 10 new { controller = "Carpool", action = "Create" } 11 ); 12 13 routes.MapRoute( 14 "DeleteCarpool", 15 "Home/Delete", 16 new { controller = "Carpool", action = "Delete" } 17 ); 18 19 routes.MapRoute( 20 "ReturnHome", 21 "Carpool", 22 new { controller = "Home", action = "Index", id = UrlParameter.Optional } 23 ); 24 25 routes.MapRoute( 26 "Default", // Route name 27   "{controller}/{action}/{id}", // URL with parameters 28 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 29 ); 30

My only question is how to add in the parameter so it looks like Carpool/Update/2 and not Carpool/Update?carpoolId = 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: