MVC is a design pattern that helps organize your code to make it more manageable and easier to maintain. The MVC acronym stands for Model, View, Controller. These are the three categories that your code will be divided into.
The Model is a code class that defines and represents the data. Most often, this is the data that resides in the database. The most common example is the data in a database table, and the properties that are in the model class are representative of the columns in the table. Here is an example of a typical model class.
As you can see in the image above, the Category class is the model that represents the Category table, and the columns in that table are Id, and Name. All the files that contain models that represent data in your application are kept inside a "Models" folder in your project. They are kept separate from the other project files and folders.
The View part of MVC represents the GUI that the user will interface with. Most commonly this will be the web pages on the application's website. Data is displayed in the web page, or view, by using what Microsoft calls Razor Syntax. You simply use the @ symbol to inject C# code, and use the "asp" attributes in elements in the view. Here is an example.
The views, or web pages are kept inside a "View" folder in the project.
The Controller part of MVC is what allows the models to communicate with the views and vice versa. Think of it like a restaurant. The user sits down and looks at the menu(front end) and sees a list of dishes(methods or actions). The waiter(controller), takes the order(action) and brings it to the kitchen(back end). The kitchen processes the request and returns the dish(data) that was requested and returns it to the user. Most commonly the controller is a class that holds methods or actions for the CRUD operations, which are Create, Read, Update and Delete. These are the common actions performed on a database. Typically you will have one controller for each model, and 4 views/pages for each of the CRUD operations.
Here is an example of a basic controller.
As above, the controller files will be collected in a Controllers folder in your project. This is a very basic explanation of how the MVC design pattern looks. Feel free to comment below or contact me if you have any questions.
0 COMMENT(S)