MVC: Custom Model

Please see my other MVC articles.

Developing a custom model to encapsulate a unique domain.

The Model serves as the heart of the MVC application, containing a domain’s business logic.
In this example, I will illustrate how to create a custom Model representing a typical sales associate within any company.
Also, I will build a Create View and a List View for displaying newly created sales associates.

First, I’ll create a new Model “SalesAssociate” within the /Models folder.
In addition to the attributes for holding its data, I’ve added validation logic to ensure required fields are enforced as well as the correct format of Email Address and Phone fields.

Now I will build the Controller that will receive requests for actions such as Create, Edit, and View.
The important thing to notice is that I selected a Template so that the wizard creates the appropriate signatures for typical actions such as Create, Edit, View, Delete, and Details.





Now that I have a Controller, it’s time to define the implementation for those actions I wish to use.
The first thing I need is a member to hold new sales associates created using the Create action/View.


Now, I’ll update the Post version of the Create action in the following ways:

  • The action’ parameter list now expects the SalesAssociate Model – this is what allow my implementation to access the Model data.
  • If the user submits data inconsistent with the Model’s business rules (empty field that’s required), the form will redisplay with the appropriate error messages.
  • If the form’s data passes validation, the new sales associate object is added to my array and the user is redirect to the List View displaying all saved sales associates.


With my Create action method ready, I will now create the View that displays the Create form to the user.
Notice that I tell indicate in the wizard that my Create form will use SalesAssociate Model and it should create for me a Create template – auto-generated Create form which contains HTML controls for each field in the Model.


My Create View is created and begins with a statement which allows the rest of the View to access the Model’s fields.


The Create form begins with a Html.BeginForm() statement – this create the Html form for the user using each of the exposed Model properties.
For example, notice for each property the “Html.LabelFor,” “Html.EditorFor,” and “Html.ValidationMessageFor” statements. These are used by the .NET Framework to generate HTML controls – a label, input control, and validation message for each property in the Model.


With the Create method and View ready, I’ll now do the same for the Index action method.
First, I’ll ensure my collection of sales associate is passed into the Index View.
This method will display created sales associates.
First, I’ll create View which is strongly-typed to the SalesAssociate Model.


Like the Create View, the beginning “@model” statement exposes the Model’s properties to the View.


The View displays data saved from the user for each property.


When I load the Index View for the first time, no sales associates are displayed because I haven’t yet saved any.


I’l use my Create View to create my first sales associate.


Upon pressing Submit, my sales associate is saved and I’m redirected to the Index View which displays my newly saved sales associate.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s