Asp.net MVC - Can I load a view from a different view folder?

前端 未结 2 2093
再見小時候
再見小時候 2020-12-16 11:13

In my app I have a need to load the same view from two different controllers without placing the view in the shared views directory.

Basically I have this folder str

相关标签:
2条回答
  • 2020-12-16 11:48

    Yes, you can. Simply return View("~/Views/Events/Preview.aspx").

    However, i would advise against it for a number of reasons. The biggest being that this will be non-obvious to anyone trying to modify the code later (maybe even you) and might lead to potential errors.

    A better approach might be to create a "Shared" view, or a shared partial view. My preference would be a shared partial view, then in your non-shared view render the partial view functionality you want.

    0 讨论(0)
  • 2020-12-16 12:01
    1. It's possible.
    2. I am not sure if you are using strong-typed views. But suppose it is, then it is a bit weird for me that you have Event search & Search with same View Model. Possibly separate them with two different view models and view would be better IMHO. Moreover, if you specify the name of view to load in controller, it is somehow considered to be coupling view and controller and it certainly not a good idea.
    0 讨论(0)
提交回复
热议问题