Is it possible to do something like this in an ASP.NET MVC controller?
public ActionResult Index(CustomADT adt)
{
...
}
And you will pass in a class from another controller when you click on a link. Or is it only possible to pass around strings?
Edit:
A bit more elaboration. Let's say I have the following class hierarchy:
public class Area
{
public string Name { get; set; }
public ICollection<Building> Buildings { get; set; }
}
public class Building
{
public string Name { get; set; }
}
So Area
contains a list of Buildings
. Now, I have two controllers, AreasController
and BuildingsController
. Both have an Index()
method. What I'd like to do is when this URL is navigated to:
It'll list all the areas. Then, when you go to this URL:
It'll list all the buildings for area with ID 1. In BuildingsController
, I receive the ID as an int and then use it to find the correct Area, like so:
public ActionResult Index(int areaId)
{
var area = areaRepository.GetById(areaId);
return View(area.Buildings);
}
Now, this seems pretty clunky to me. The areaId has to be received as an int, then I have to hit up the repository again to get the actual Area
object. Is there any way that I can do this instead:
public ActionResult Index(Area area)
{
return View(area.Buildings);
}
And not hit up the repository again and re-retrieve an object that's already been instantiated? I'm leaning towards no because of how HTTP works (you can't place an object in the URL), but maybe someone has a neat trick up their sleeve.