Sie sind auf Seite 1von 20

Click to edit

Master subtitle
style

Creating and Configuring Models


Jon Galloway | Development Platform Evangelist
Christopher Harrison | Content Developer

Module Overview
What is a Model?
Creating Models
Advanced Model Concepts

Click to edit
Master subtitle
style

What is a Model?

What is a Model?
A class
No really, thats it. Its just a class.
We promise its just a class.

DEMO
Adding a Model

Click to edit
Master subtitle
style

Creating Models

Who Uses the Model?


User
What data will they be interacting with?

MVC
Input controls
Display formatting
Validation

Preparing Models
Attributes
Decorate properties

Available Attributes
DataTypeAttribute
DisplayAttribute
Validation

RequiredAttribute
StringLengthAttribute
RegularExpressionAttribute
CompareAttribute

Data Types
Validation
Input controls
HTML5

Available Data Types


CreditCard
Currency
EmailAddress
Password
Url

DEMO
Adding DataType Attribute

Prompts
Display Attribute
Name
Display prompt and header

DisplayFormat
Uses a .NET format string
{0:C}
Currency

{0:0.00}
Always use two decimal places
.42 displays as 0.42
42 displays as 42.00

{0:d-m-yy}
Single digit day & month, two digit year
June 10th, 2014 displays as 10-6-14

http://msdn.microsoft.com/en-us/library/system.string.for
mat(v=vs.110).aspx
See the Q&A

DEMO
Controlling Display

Adding Validation
Attributes
Required
StringLength
MinLength
MaxLength

RegularExpression
Range

Error Message
{0} will use the display name
"{0} must be provided"

Where Does Validation Occur?


On the server
ModelState.IsValid property

On the client
Requires jQuery unobtrusive validation

DEMO
Adding Validation

Click to edit
Master subtitle
style

Advanced Model Concepts

How Models Make Life Easier


Automatic UI creation
Model Binding
Controllers are classes
Actions are methods
Methods take parameters
MVC will convert forms to parameters

Advanced Model Concepts


Custom Validation Attributes
When the built-in attributes arent good enough
Integrates with MVC

View Model
Differences between domain and user interface
Complex data
Dropdown lists
Unrelated objects

2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered
trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of
Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a
commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT
MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Das könnte Ihnen auch gefallen