我有一个MVC5库存web应用程序,其中有一个项目列表和每个项目的交付按钮。
@foreach (var i in Model.Inventario)
{
@using (Html.BeginForm("AggiungiAlloScarico", "Scarico"))
{
string Disabilitato = "";
string Classe = "";
// some method to define the buttons' styles
<div class="pull-right">
@Html.Hidden("Modello", i.Item.Modello)
@Html.Hidden("returnUrl", Request.Url.PathAndQuery)
<input type="submit" class="@(String.Format("{0}", Classe))" value="Consegna" @Disabilitato />
</div>
}
}
只要点击其中任何一个按钮
AggiungiAlloScarico
中的动作方法
Scarico
应触发控制器。此方法只是将所选项目添加到会话中的对象,然后将用户重定向到
Index
[HttpPost]
public RedirectToRouteResult AggiungiAlloScarico(string Modello, string returnUrl)
{
InventoryItem item = itemRepository.Inventario.FirstOrDefault(i => i.Item.Modello == Modello).Item;
if (item != null)
{
GetScarico().AddItem(item, 1);
}
return RedirectToAction("Index", new { returnUrl });
}
问题是这个动作方法根本没有被触发。我认为路由可能有问题,因为每次我按“Consegna”按钮时,都会被重定向到
localhost:port/Scarico/AggiungiAlloScarico
page:该url构造对应于默认的路由映射(RoutConfig类中的最后一个方法),但是没有调用相关的操作。此外,我在方法上设置了一个断点,但它从未停止,所以我猜问题出在这里,但我真的看不到它。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
null,
"",
new { controller = "magazzino", action = "inventario", manufacturer = (string)null, page = 1 }
);
routes.MapRoute(
null,
"Page{page}",
new { controller = "magazzino", action = "inventario", manufacturer = (string)null },
new { page = @"\d+" }
);
routes.MapRoute(
null,
"{manufacturer}",
new { controller = "magazzino", action = "inventario", page = 1 }
);
routes.MapRoute(
null,
"{modello}",
new { controller = "magazzino", action = "inventario", manufacturer = (string)null, page = 1 }
);
routes.MapRoute(
null,
"{manufacturer}/Page{page}",
new { controller = "magazzino", action = "inventario" },
new { page = @"\d+" }
);
routes.MapRoute(
null,
"{manufacturer}/{modello}",
new { controller = "magazzino", action = "inventario", page = 1 }
);
routes.MapRoute(null, "{controller}/{action}");
}
编辑:
我把所有的路由都注释掉了,除了默认的路由,它是有效的。我的定制路线怎么了?
感谢您的帮助。
谢谢,
戴维德。