Tridip Bhattacharjee

Tridip Bhattacharjee

  • NA
  • 1.2k
  • 78.1k

ASP.Net MVC:clicking on routelink url not passing all the da

Feb 5 2018 8:51 AM
i have creating url using routelink.
  1. @Html.RouteLink("Edit""PageWithId",  
  2. new  
  3. {  
  4. controller = "Customers",  
  5. action = "Edit",  
  6. id = item.CustomerID,  
  7. page = ViewBag.CurrentPage  
  8. })  
i am using this routing PageWithId with route link
  1. routes.MapRoute(  
  2. name: "PageWithId",  
  3. url: "{controller}/{action}/{page}/{id}",  
  4. defaults: new { controller = "Customers", action = "Edit", page = UrlParameter.Optional, id = UrlParameter.Optional }  
  5. ); 
i have 3 routing code. here is all
  1. routes.MapRoute(  
  2. name: "PageWithSort",  
  3. url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}",  
  4. defaults: new { action = "Index", page = UrlParameter.Optional, SortColumn = UrlParameter.Optional, CurrentSort = UrlParameter.Optional }  
  5. );  
  6.   
  7. routes.MapRoute(  
  8. name: "PageWithId",  
  9. url: "{controller}/{action}/{page}/{id}",  
  10. defaults: new { controller = "Customers", action = "Edit", page = UrlParameter.Optional, id = UrlParameter.Optional }  
  11. );  
  12.   
  13. routes.MapRoute(  
  14. name: "Default",  
  15. url: "{controller}/{action}/{id}",  
  16. defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  17. ); 
when i run my program the route link generate url like http://localhost:55831/Customers/Edit/1/ALFKI

when i click on the link the Edit action is getting called but customer id is getting null where as ALFKI is there in url as customer id.

here is my edit action details
  1. public ActionResult Edit(string id, int page)  
  2. {  
  3. if (id == null)  
  4. {  
  5. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  6. }  
  7. Customer customer = db.Customers.Find(id);  
  8. if (customer == null)  
  9. {  
  10. return HttpNotFound();  
  11. }  
  12. ViewBag.CurrentPage = page;  
  13. return View(customer);  

please tell me why id is getting null when ALFKI as passing as customer id?

thanks

Answers (1)