Understanding the Model

Introduction

In ASP.NET MVC framework, the model is the portion of the application responsible for the core logic. Model objects typically access data from a persistent store, such as SQL Server, and perform the business logic on that data. Models are application specific, and therefore the ASP.NET MVC framework places no restrictions on the kinds of model objects you can build. For instance, you can use ADO.NET DataSet or DataReader objects. You could use a custom set of domain objects. You can also use a combination of objects types to manipulate data.

The model is not a specific class or interface. A class is part of the model not because it implements a certain interface or derives from a certain base class. Instead, a class is part of the model because of the role that the class plays in the ASP.NET MVC application, and where the class exists in the architecture of the application. A model class in an ASP.NET MVC application does not directly handle input from the browser, nor does it generate HTML output to the browser.

Defining the Model

Model objects are the parts of your application that implement the domain logic, also known as business logic. Domain logic handles the information between your database and the UI. For example, in an inventory system, the model keeps track of the items in storage. The model also contains the logic to determine whether an item is in stock. In addition, the model would be the part of the application that updates the database when an item is sold and shipped out of the warehouse. Often, the model also stores and retrieves model state in a database.

Integrating the Model and Controller

The Models subdirectory that is provided by the ASP.NET MVC application template is a convenient place to place your model classes. It is also quite common to place model classes in a separate assembly, so that you can reuse these classes in different applications.

Using model classes from a controller typically consists of instantiating the model classes in controller actions, calling methods of model objects, and extracting the appropriate data from these objects to display in your views. This is the recommended approach for implementing actions. It also maintains separation between the logical elements of the application, which makes it easier to test the application logic without having to test it through the user interface.

This topic is ASP.NET 3.5 Extensions pre-release documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases. To provide feedback or ask questions about the release, please go to the ASP.NET 3.5 Extensions forums.