Can two different controllers access the same view in mvc?

I have two different controllers, and I want both of them to use a common view.

Is it possible?

Thanks in advance!!!!

+5
source share
1 answer

Yes . Specify the full view path in the method View.

public class UserController : Controller
{
   public ActionResult ShowUser()
   {
     return View();
   }
}
public class AccountController : Controller
{
   public ActionResult ShowAccount()
   {
     return View("~/Views/User/ShowUser.cshtml");
   }
}

If the name of your views is the same for both controllers, you can save the general view in a directory Views/Sharedand simply call the View method without any parameters. The name of the view must be the same as the name of the method Action.

public class UserController : Controller
{
   public ActionResult ShowUser()
   {
     return View();
   }
}
public class AccountController : Controller
{
   public ActionResult ShowUser()
   {
     return View();
   }
}

Assuming you have a view called ShowUser.cshtmlin a folder Views/Shared.

+15
source

All Articles