APRess ASP.NET MVC 4 (2)

Adding a business class
e.g., a class in charge of sending email messages to pet owners. Or, a class that creates image thumbnails. In short, any type of processing on the server is handled by business model classes. (Similar to APP_CODE in Web Site Project)
Under Models, add a new folder: Business. And under the folder create a new class: PetManagement.cs.
image
image
Create a view model: Picture Model to pass between controller and view:
image
Then create the view
image
This line:
image
adds a anti-forgery token in HTML like this:
image

This works only when the server side action also has a annotation tag placed on top
(This is the POST Method)
image
After this you can upload and save the file.
Understand Model Binding
GET request – saved in HttpRequest.QueryString
POST request – saved in HttpRequest.Form
POST/File Upload request — saved in HttpRequest.Files

Model Binding:
1) No manual code is needed to extract the data from the HTTPRequest object.
2) Data type conversion happen automatically.
3) Data can be easily validated

Model Binding is possible thanks to the built-on DefaultModelBinder.
It relies on the “names” of the values – they must be the same as defined in the model object.
Chapter 7 – Data Validation (Page 94)
We have a MessageModel
image
Now we need a controller. MessageController.
image

ModelState.AddModelError has two parameters –
(1) specific property from the view model for which the error is generated. Can be empty if this is a generic error.
(2) error description.
Next, create a GET (Form) view:
image
Now the form is created, but there is no validation checking because there is no data annotation defined in the model and there is no manual checking.
image
Now we add some manual checking
image
Now if you submit the form with an empty “From” field, you get this error:
image
Validate with Data Annotation Benefits:
1) enforced across the whole application, not just a single action.
2) no need to call a method, or do anything to enforce the rule
3) code is much less than coding manually
image
Other annotation:
[CreditCard] is 15 or 16 digits without spaces
[EmailAddress] is a well-formed email address
[EnumDatatype(typeof(name of enum)] enable a .NET framework enum be mapped to a data column. The value must be of those defined in the enum.
[FileExtension(Extensions=”.jpg,.png,.gif”)] file extension must be in one of them
[Range(min, max)] the value must be in the range.
[RegularExpression(pattern)] must match pattern
[Required] is required field
[StringLength(max)] is max length
[URL] must be a well-formed URL
Once you added the notations, the validation will validate the inputs:
image
Custom Data Annotations
To create a custom data annotation (for example, [BirthCertificateNumber]) you need to create a class that inherits from ValidationAttribute. Name of the class must end with Attribute.
Create a folder under /Models, used to save all custom annotations.
image

under the new folder, create a new class: BirthCertificateNumberAttribute.cs.
image
Then in the MessageModel, add a new property so we can test it:
image
In the GET form, add the form fields for the new property so we can test the validation
image
Now you see this form: (new field with the [BirthCertificateNumber] Annotation.
image
If you enter something that’s not valid for this format, you see this error message (custom validations are performed only on the server side!!!! No javascript validation)
image
Page 110

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s