0

我想向我的用户提供这两个 URL:

/Account/Orders   <- This would should be a grid of all orders.
/Account/Orders/132   <- This would show this particular order information.

这是我的两个 ActionMethods:

[Authorize]
public ActionResult Orders(int id)
{
    using (var orderRepository = new EfOrderRepository())
    using (var accountRepository = new EfAccountRepository())
    {
        OrderModel model = new OrderModel();
        return View(model);
    }
}

[Authorize]
public ActionResult Orders()
{
    using (var orderRepository = new EfOrderRepository())
    using (var accountRepository = new EfAccountRepository())
    {            
        List<OrderModel> model = new List<OrderModel>();
        return View(model);
    }
}

如果我的 Orders 视图使用 anOrderModel作为模型进行强类型化,则Orders()action 方法将不起作用,因为我需要将 IEnumerable 而不是单个对象传递给它。

在这种情况下你有什么建议?这似乎很容易做到,但我度过了很长(富有成效!)的一天,但我只能走这么远。

4

3 回答 3

1

假设您使用的是默认路由,Order则永远不会调用您的第二种方法。如果没有提供一个空值,该路由将用空值填充缺少的id参数,并尝试使用该id参数调用重载。

你可以改变你的路线或做其他事情来尝试解决这个问题,但更快的选择是只在路由系统中工作:

public ActionResult Orders(int id = -1)
{
  return id == -1 ? this.OrdersSummary() : this.OrdersDetail(id);
}

private ActionResult OrdersSummary()
{
  var model = new SummaryModel();
  // fill in model;
  return this.View("OrdersSummary", model);
}

private ActionResult OrdersDetail(int id) 
{
  var model = new DetailModel();
  // fill in model;
  return this.View("OrderDetail", model);
}    
于 2012-04-07T02:06:54.273 回答
1

几个选项:

1)首先设置最具体的路线

MapRoute("first", "/Accounts/Orders/{id}" ....
           controller="mycontroller" action="details"
MapRoute("second", "/Accounts/Orders .....
           controller="mycontroller" action="summary"

2)而不是路由有两个具有不同签名的get方法:

public ActionResult Index()
{
}

[ActionName("Index")] public ActionResult IndexDetails(int id) { }

路由应该匹配

于 2012-04-07T02:42:17.067 回答
0

你可以有两种不同的观点。一个用于网格,一个用于订单详细信息。

然后你可以这样称呼他们:

return View("OrderGrid", Orders); // for grid

return View("OrderDetail", Order); // for detail
于 2012-04-07T01:42:57.907 回答