Thursday 28 January 2021

Part 13: Model Controller and View

#5 minutes of reading

In this article we will discuss in detail about practical use of Model, View, and controller. I’m using here project created in previous blog.

 

Model:

Model is nothing but mock of database table, lets understand in more details.

    

Suppose I have table in my database with 5 columns Id, firstName, lastname, Grade and City.



Above is database table and if we want to create a complex type of the above table in MVC, model is used as discussed Model is mock for database table.


We have created model and database column here represent as class property (Id, FirstName,  ..etc). Later we can use this Model to hold data coming from database table. Now we clear why model is used. There is no compulsion model only created for creating database table replica, but you can create model for the purpose to show data on razor view. Data in model from anywhere created on frontend, backend (database, files, etc).

 

Add controller class with name Student.


Add piece of code to bind data to model and return to view. In real time binding data to view is not performed on controller and dependency injection or repository pattern use to bind data. Will discuss in detail in future article.

Add below piece of code in Index action method of Student.



Create strongly typed view using model(student) created.

 


Index view for Student controller gets created and code in index view gets created automatically with the help of MVC scaffolding.

MVC model called as strongly typed model if they

 


View called as strongly typed view if @model property declared at the top of View. Code gets automatically generated for strongly typed view with the help of MVC scaffolding.


You can also create/manage html later in strongly typed view or create normal view.

Run application with controller name Student in URL and check browser.




Previous                                                                                                                                                 Next

 


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home