ASP.NET MVC Model View Controller Example

82
asp.net mvc model view controller example

How the model view and controller communicate with each other in ASP.NET MVC

In this article, I will demonstrate asp.net MVC Model View Controller example and we will learn how the model view and controller communicate with each other in the MVC framework. We have already created StudentController, model, and view in the previous articles, but we have not integrated all these components in order to run it.

The following code snippet shows StudentController and Student model class & view created in the previous articles.

Example: StudentController

Example: Student Model class

Example: Index.cshtml to display student list


Now, to run it successfully, we need to pass a model object from controller to Index view. As you can see in the above Index.cshtml, it uses IEnumerable of Student as a model object. So we need to pass IEnumerable of Student model from the Index action method of StudentController class as shown below.

Example: Passing Model from Controller

As you can see in the above code, we have created a List of student objects for an example purpose (in real life application, you can fetch it from the database). We then pass this list object as a parameter in the View() method. The View() method is defined in the base Controller class, which automatically binds a model object to the view. Now, you can run the MVC project by pressing F5 and navigate to http://localhost/Student. You will see the following view in the browser.

index view

  • For more refer to MSDN

Conclusion

I hope you liked this article about how to create MVC Model View Controller with an example. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

LEAVE A REPLY

Please enter your comment!
Please enter your name here