Conventions in MVC

Controllers

There are several conventions to be followed by every MVC application. For example, every user-defined controller needs to have the word Controller suffixed with the name of the controller, in other words HomeController or BusinessController and all these user-defined controllers must implement the IController interface directly or indirectly. But whenever we add a new controller file you will see all the controllers inherit from the Controller class.

Controller

So, why are we not getting a complier error?

Because this Controller's base class that our user-defined controller inherits from implements the IController interface.

interface

Views

Now let's see the conventions to be followed by Views.

Every view that we create must or should be placed in a location where MVC can find them. To understand, let's look at a very simple example.

Currently in my project I have a controller named “HomeController” with the following code in the Index action method.

Note

If you are new to ViewBag and ViewData, then I strongly recommend you read ViewBag and ViewData in MVC.

ViewBag

To transfer these data from the controller to a view, we are using the ViewBag dynamic object.

We have our data and using ViewBag we are passing the data to the view. But before passing the data to the view, we must add a view and for that right-click on the Index action method and click Add view.

Provide the name “Index” and click OK.

Index

Now go to the Solution Explorer and expand the Views folder, you will see the Index.cshtml that we have created present in another folder, in other words Home and Home is nothing but the name of the controller.

explorer

In the Index.cshtml write the following:

In the Index

Run the application

Run the application

We got the brand names in a list format.

But what will happen if I change the Home directory from Home to Contact or the Index.cshtml to IndexOne.cshtml?

We will get an exception.

exception

So, that's why the convention is important when controllers and views are concerned.

Models

With models, there are not strict rules for the conventions. In fact, the Models folder is optional and can be present in a separate project.

Let's look at an example.

Currently in the project, I have this Models folder.

Models

Now what I will do is, I will do something with this folder from our main project.

main project

If we build our application, we won't get an error.

Now with our main project we will add another project to the same solution.

Right-click on the solution then click add -> New project.

new project

Select the project as class library then provide a name then click OK.

class library

The class library will look like this.

library

Change the class1.cs to Brands and write the following in it.

class1

Build the application

Now let's use this library in our main project and for that we need to add the reference to the class library project.

Right-click on references then click Add reference.

add reference

Expand the solution then projects then select the project then click OK.

select the project

So, this class library is now the Model of our MVC application and we can add all the business logic or code to retrieve the data from the database or from any other data source here.

Thank you.

Up Next
    Ebook Download
    View all
    Learn
    View all