MVC: Custom Model

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.

2-mvc-custom-model

 

3-mvc-custom-model

 

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.

4-mvc-custom-model

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.

5-mvc-custom-model

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.

6-mvc-custom-model

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

7-mvc-custom-model

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.

8-mvc-custom-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.

9-mvc-custom-model

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

10-mvc-custom-model

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

11-mvc-custom-model

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

12-mvc-custom-model

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

13-mvc-custom-model

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

14-mvc-custom-model

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s