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.
- It's possible.
- 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.
精彩评论