Search code examples
c#asp.net-mvc-3actionmethod

Confused about how Controllers actually map to views in this case


I have these two URL's I want to make available to my users:

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

Here are my two 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);
    }
}

If my Orders view is strongly typed with an OrderModel as the model, the Orders() action method won't work as I need to pass it an IEnumerable instead of a single object.

What do you suggest in this case? This seems like something that's easy to do, but I've had a very long (productive!) day, but I can only go so far.


Solution

  • A couple of options:

    1) Setup your routes with the most specific one first

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

    2) Instead of routes have two get methods with different signatures:

    public ActionResult Index()
    {
    }
    

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

    Routing should match the