Search code examples
asp.net-mvccontroller-actions

ASP.NET MVC View Issue when returning ActionResult from different Action


I have two ASP.NET MVC Actions:

public ActionResult GetAll()
{
      return GetOne(1);
}

public ActionResult GetOne(Int32 id)
{
      return View(id);
}

As you can see, GetAll is calling the action GetOne. However, when GetAll() is called (calling GetOne(id) and should be returning GetOne view) MVC throws an error saying that there is no GetAll view. Huh?

How can I have GetAll call GetOne and use GetOne's view (which I thought was the logical thing to happen to begin with)?


Solution

  • public ActionResult GetOne(Int32 id)
    {
          return View(id, "GetOne");
    }
    

    Specifying the view name explicitly overrides the default, which is to use the action key in the route values collection, which is equal to "GetAll" in this case.