Asp.net MVC - 我可以加载不同的视图文件夹视图? [英] Asp.net MVC - Can I load a view from a different view folder?
问题描述
在我的应用我有一个需要加载从两个不同的控制器相同的观点,而不放置视图中的共享目录的意见
基本上我有这个文件夹结构
- 控制器
- EventsController.cs
- SearchController.cs
- 正文
- 活动
- preview.aspx
- 搜索
- 活动
基本上图片吧大致相同这里堆栈溢出。你得到了一堆的问题链接下的问题的preVIEW,但是当你在搜索栏中输入搜索也得到相同格式的页面。的观点和的ViewModels是presumably相同的。
因为我需要对搜索的看法是完全一样的,因为我需要对事件的看法,我想重复使用相同的看法。不过,我想,以避免使用共享目录为这个特定视图。
所以我的两个部分的问题是---
- 这是可能的,如果又如何?
- 这是不好的做法?
是的,可以。只要返回查看(〜/查看/活动/ preview.aspx)
。
不过,我建议不要它有许多原因。最大的幸福,这将是非显而易见的任何人试图以后修改code(甚至你),并可能导致潜在的错误。
有一个更好的办法可能是建立一个共享视图,或共享的局部视图。我的preference将是一个共享的局部视图,然后在您的非共享视图呈现你想要的局部视图功能。
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
- SearchController.cs
- Views
- Events
- Preview.aspx
- Search
- Events
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 ---
- Is this possible, and if so how?
- Is this bad practice?
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.
这篇关于Asp.net MVC - 我可以加载不同的视图文件夹视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!