例如,你有这样的自定义路线:
CustomerOrder/{action}/{id}/customerid={customerid}
网址变成这样:
CustomerOrder/Create/customerid=1
如何获取customerid并在视图中使用它?
<%= Html.MenuItem("Back to List", "Index", new { customerID = ???????? })%>
答案 0 :(得分:3)
等号会混淆网址解析器,因为它具有特殊含义。
如果您要将路线更改为:
routes.MapRoute("CustomerOrder", "CustomerOrder/{action}/{id}",
new { controller = "Order", id = "" });
然后是以下视图代码
<%= Html.MenuItem("Back to List", "Index", new { customerID = 5 })%>
会创建一个指向:
的链接CustomerOrder/Index/?customerid=5
哪个会好起来的。
注意强>
鉴于您当前的路由配置,您可以通过删除您的CustomerOrder路由获得完全相同的结果,因为它已损坏,您可以从默认路由获得所需的结果。
答案 1 :(得分:0)
继承Global.ASAX中的代码
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("CustomerOrders", "CustomerOrder/{action}/{id}/customerid={customerid}",
new { customerid= "{customerid}", controller = "CustomerOrder", action = "Index", id = "" });
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" });
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("CustomerOrders", "CustomerOrder/{action}/{id}/customerid={customerid}",
new { customerid= "{customerid}", controller = "CustomerOrder", action = "Index", id = "" });
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" });
}
答案 2 :(得分:0)
我是MVC的新手,但我认为控制器获取了一个customerid参数,可以传递给View(通过ViewData),不是吗?