Problem with ViewBag and routes (MVC 3 - RC2)

I am having a problem with MVC-3 generating outbound routes for me.

This is the address of the page on which I am enabled for both scenarios: http: // localhost: 1283 / Conflict / Create / 1200/300

Here are the map routes:

routes.MapRoute( null, // Route name "{controller}/{action}/{custId}/{projId}", // URL with parameters null, // Parameter defaults new { custId = @"\d+", projId = @"\d+" } ); routes.MapRoute( null, // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); 

Scenario 1:

From the controller:

 public ActionResult Create(int custId, int projId) { return View(); } 

From point of view:

 @Html.ActionLink("Back to List", "Index", "Conflict", new { custId = ViewBag.custId, projId = ViewBag.projId }, null) 

The resulting link that is being created.

http: // localhost: 1283 / Conflict? custId = 1200 & projId = 300

If I changed the controller code as follows:

 public ActionResult Create(int custId, int projId) { ViewBag.custId = custId; ViewBag.projId = projId; return View(); } 

I did not make any changes to the view, added only these two lines to the controller and created the following link:

http: // localhost: 1283 / Conflict / Index / 1200/300

What am I missing here? This is a consistent behavior, I was able to reproduce this in other areas of my application. The "solution" is obvious, but my question is: why?

+4
source share
1 answer

What happens when the part of the link "? CustId = 1200 & projId = 300" matches the link that you used to get the page you are on. So calling Html.ActionLink does this:

  • create path / Conflict / Index
  • find custId and projId in the ViewBag and find the query string
  • just adds a query string

In the second scenario, you are actually providing values, so the link is generated normally. Hope this helps.

+2
source

Source: https://habr.com/ru/post/1332612/


All Articles