net MVC -我可以从不同的视图文件夹加载视图吗?

时间:2022-03-03 06:10:31

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 structure

基本上我有这个文件夹结构。

  • Controllers
    • EventsController.cs
    • EventsController.cs
    • SearchController.cs
    • SearchController.cs
  • 控制器EventsController。cs SearchController.cs
  • Views
    • Events
      • Preview.aspx
      • Preview.aspx
    • 事件Preview.aspx
    • Search
    • 搜索
  • 预览视图事件。aspx搜索

basically picture it much the same as here on stack overflow. You get a preview of a bunch of questions under the questions link, but you also get an identically formatted page when you do a search in the search bar. The views and viewmodels are presumably identical.

基本上想象它和这里的栈溢出差不多。你可以在问题链接中预览一堆问题,但当你在搜索栏中进行搜索时,你也可以得到一个格式相同的页面。视图和视图模型应该是相同的。

Since the view I need for search is exactly the same as the view I need for events, I'd like to reuse the same view. I would however like to avoid using the shared directory for this specific view.

由于搜索所需的视图与事件所需的视图完全相同,所以我希望重用相同的视图。但是,我希望避免为这个特定的视图使用共享目录。

So my two part question is ---

我的两个问题是---

  1. Is this possible, and if so how?
  2. 这是可能的吗?
  3. Is this bad practice?
  4. 这是不好的做法吗?

2 个解决方案

#1


33  

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

是的,你可以。仅返回视图(“~ /视图/事件/ 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.

更好的方法可能是创建“共享”视图,或者共享部分视图。我的首选是共享部分视图,然后在非共享视图中呈现所需的部分视图功能。

#2


1  

  1. It's possible.
  2. 这是有可能的。
  3. 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.
  4. 我不确定您是否使用强类型的视图。但假设它是,那么你有事件搜索和搜索与相同的视图模型对我来说有点奇怪。可能用两个不同的视图模型和视图将它们分开会更好。此外,如果您在controller中指定要加载的视图的名称,它在某种程度上被认为是视图和控制器的耦合,这当然不是一个好主意。

#1


33  

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

是的,你可以。仅返回视图(“~ /视图/事件/ 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.

更好的方法可能是创建“共享”视图,或者共享部分视图。我的首选是共享部分视图,然后在非共享视图中呈现所需的部分视图功能。

#2


1  

  1. It's possible.
  2. 这是有可能的。
  3. 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.
  4. 我不确定您是否使用强类型的视图。但假设它是,那么你有事件搜索和搜索与相同的视图模型对我来说有点奇怪。可能用两个不同的视图模型和视图将它们分开会更好。此外,如果您在controller中指定要加载的视图的名称,它在某种程度上被认为是视图和控制器的耦合,这当然不是一个好主意。