Showing posts with label Model. Show all posts
Showing posts with label Model. Show all posts

Monday, March 4, 2019

Binding Model to View and Display data

In the previous post we saw on how to create a Model class, in this post we will use the model class in the controller to populate the model properties and pass on a list of Model objects to the view layer to display the data in the view.

In the controller we will instantiate the model class set values to its properties and bind it with the view to display the data. Next let us edit the Home controller and initialize the model object as follows.

Adding Model class to Asp.Net Core MVC

In the previous post we saw a basic Hello Word sample for MVC application using Asp.Net core. Let us now do some more improvement to the example and add a Model class to the project. Model is a Domain object or Business object which contains properties to represent the object. For example Product is a model class which contains properties like ProductName, Number, Cost etc.

Model is one of the layers of the MVC architecture, in MVC model deals with the data / structure of the business objects. Model objects are populated in the controller and passed on to the View layer for display. Let us not add a Model class Product to the MVC project. To do this right click on Model folder -> Add -> click on Class. Let us name our class Product.cs. Add the following code to the Model class.