• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Conventions Used In MVC to Find View

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this article we will understand How Conventions Used In MVC to Find View By Sagar Jaybhay.



See the above image in this we are creating Index methods below is code for that.

   
In the above code, we don’t use view names we simply use view and pass employee object to that view. So the question is how asp.net MVC knows which view is call and display?

Which location asp.net MVC used to find view. For understanding this we create one method in our EmployeeController whose name is wheretofindview and simply return View is used but we never created a view for that by doing this we will understand from which location view is called.




See the first line by convention it is finding wheretofindview as view name and location which is found

   First, it is searching in views folder in that controller specific folder in our case it is EmployeeController. So it will try to find a view in the Employee folder.
   If in controllers folder it not found then it will go for Shared folder inside the Views folder.

Means Location where to find the view is

   Views / Controller name folder
   Views / Shared folder

As Per View Extension

   Aspx
   Ascx:- partial view
   Cshtml :- razor view
   Vbhtml:- razor view



If your View name is different from the action name then you need to specify the full path in View to get that view as given below.


Summary

   MVC is Convention over configuration
   View folder contains a folder for every controller in our application along with a shared folder
   A shared folder is used to share views that are used for more than one controller. Mainly master page and layout page are stored in a shared folder.

GitHub :- https://github.com/Sagar-Jaybhay/MVC5
 
Saloon Keeper
Posts: 28314
207
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're mistaking file organization conventions for some sort of general functionality.

One of the great strengths of the Model/View/Controller architecture is that Models and Views don't have to have a 1-to-1 correspondence. A classic example I like to use is where you have a table full of data (Model) and you wish to display that data both in spreadsheet form and as a pie chart at the same time. In such a case, you have one Model, but two Views (which may or may not be sub-views of a single parent View).

What binds Models and Views are Controllers. Depending on the GUI system in use, a Model or a View may have no idea what Controller(s) reference them, although Controllers do have do know which Models and Views they are linking together. So if you can determine, given a Model, what its Controller(s) are, you can potentially find the View(s) of that Model. But that would depend on the GUI framework in use.

Otherwise, about the best you can do is hope that a decent file naming and organization scheme was used when the source program was created.
 
This will take every ounce of my mental strength! All for a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic