I have a small project very basic, VS 2013, .Net Framework 4.5 with System.Web.Mvc (5.2.7) installed.
No special routes or authentication; just 3 controllers scaffolded from an EF context. One of the controllers is called DataRoutesController, and another is DataRouteViewFieldsController. DataRouteViewFields is a child (foreign key) of DataRoute. On the Edit page for DataRoute I have a table which lists the related ViewFields, each row with it's own edit and delete buttons.
<button class="btn btn-sm btn-primary"
onclick="location.href='@Url.Action("Edit", "DataRouteViewFields", new { id = item.RouteId, name = item.Name })'"
title="Edit">
<span class="glyphicon glyphicon-pencil"/>
</button>
When I click the button it calls the correct controller and proceeds to the return statement.
public ActionResult Edit(int? id, string name)
{
DataRouteViewField dataRouteViewField = db.DataRouteViewFields.Find(id, name);
return View(dataRouteViewField);
}
But then, for some reason I can't comprehend I calls the DataRouteController Index() route and returns the wrong page.
public ActionResult Index()
{
var dataRoutes = db.DataRoutes.Include(d => d.Connection);
return View(dataRoutes.ToList());
}
This behavior is not observed when I don't use the button. If I type the URL to edit the ViewField manually, it works as expected.
When I step though the code (F11) following the return from DataRouteViewFieldController.Edit(...), it proceeds to _ViewStart, then to the Edit.cshtml for DataRouteViewFieldController like it should. When it finishes Edit.cshtml it renders the wrong page. The Edit.cshtml page is largely blank for troubleshooting purposes:
@model ctNetData.DataRouteViewField
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
If I add a break to DataRoutesController.Index() then the process starts flipping back and forth between lines of code in DataRouteViewFieldsController.Edit(...) and DataRoutesController.Index() with F11, like it's processing two threads at the same time.
Anyone recognize what's going on here?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}