Question

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.

Was it helpful?

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

OTHER TIPS

Assuming you are using the default routes, your second Order method will never be called. The route is going to fill in the missing id parameter with a null value if one is not supplied, and try to call the overload with the id parameter.

You could change up your routes or do other things to try and get around this, but the faster option is to just work within the routing system:

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);
}    

You can have two different views. One for the grid and one for the order detail.

Then you can call them like this:

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

return View("OrderDetail", Order); // for detail
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top