0

I'm a very beginner(Learner) in MVC. Is this question is stupid, sorry for that. I'm having three controllers

  • Home
  • Main_Menu
  • Booking

Added view for this controller and its having three sub folder views

  • View/Home/Home.cshtml
  • View/Main_Menu/Main_Menu.cshtml
  • View/Booking/Booking.cshtml

Is it possible to put all view into one sub folder like this

  • View/Home/Home.cshtml
  • View/Home/Main_Menu.cshtml
  • View/Home/Booking.cshtml

Is this stupid? Is this bad approach?

Ayyappan Sekaran
  • 986
  • 4
  • 12
  • 27
  • 1
    it is convention to be have Folder named for each controller in Views, mvc by default maps it that way – Ehsan Sajjad Sep 16 '15 at 05:57
  • you can always do this : `return View("~/View/Home/Home.cshtml");` – mahlatse Sep 16 '15 at 05:59
  • 2
    While possible its a poor approach. MVC uses conventions to search for the view associated with the methods in each controller and you would need to write you own custom view engine. In any case views relate to action methods in controllers, not to the controllers themselves so `Views/Booking/Booking.` makes no sense. You would have view such as `Views/Booking/Index` to display a collection of bookings and `Views/Booking/Create` to create a new booking and `Views/Booking/Details` to display details of an extisng booking –  Sep 16 '15 at 06:00
  • @EhsanSajjad thank you. Can we change default map to custom? – Ayyappan Sekaran Sep 16 '15 at 06:00
  • you can but you should not, why you want that? – Ehsan Sajjad Sep 16 '15 at 06:01
  • @StephenMuecke thank for your valuable comments. – Ayyappan Sekaran Sep 16 '15 at 06:06
  • @EhsanSajjad On create every controller i felt view folder becomes too large with clumsiness. Thank you for your valuable comments. – Ayyappan Sekaran Sep 16 '15 at 06:09
  • 1
    no it becomes easy to find particular controller related views – Ehsan Sajjad Sep 16 '15 at 06:10
  • @EhsanSajjad hmm.. I'm learner in MVC. I'm having so many doubts that whether i'm going in a right approach or not. So only i asked this stupid question. Thank you and sorry once again. – Ayyappan Sekaran Sep 16 '15 at 06:18
  • You can try out areas if you feel the views are clumsy – staticvoidmain Sep 16 '15 at 06:25
  • @staticvoidmain Thank you. Area is to maintain folder for view,model and controller. I need to maintain all view into one view subfolder with different controller. – Ayyappan Sekaran Sep 16 '15 at 06:35
  • @SaiAyyappsSekaran - Yeah putting all the views in one folder violates separation of concerns. That is the core concept we move for mvc. If you want to move views inside a particular folder move along with the controllers. – staticvoidmain Sep 16 '15 at 07:15

1 Answers1

0

ASP.NET MVC already have such all in one folder - Views/Shared

If you put your view there and call Controller, that don't have View in it's own folder you get the View from Shared.

Also, you can try to change View location by creating Custom View Engine but it's not so easy. You can find how to do it here.

Community
  • 1
  • 1
teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • On creating controller(home), one empty view folder(Home) created by default. On Add view from controller, view(Home.cshtml) created into that sub folder. how to avoid this and add to shared. – Ayyappan Sekaran Sep 16 '15 at 07:02
  • 1
    @SaiAyyappsSekaran i gess you using scarfolding. So this folders creating automaticaly. But you always can replace your View to shared and delete empty folder that you don't need anymore – teo van kot Sep 16 '15 at 07:04
  • Then how the controller will return to that view?? – Ayyappan Sekaran Sep 16 '15 at 07:08
  • 1
    @SaiAyyappsSekaran it's default MVC behavior [read this article](http://theshravan.net/blog/configure-the-views-search-locations-in-asp-net-mvc/) - it shows how you can extent and overload this mechanism so it should help you t understand. – teo van kot Sep 16 '15 at 07:13
  • I really think you should edit and remove "but it's not so easy." That's subjective and discourages looking into it. It completely easy IMO. – xr280xr Aug 29 '17 at 14:28