Presentation is loading. Please wait.

Presentation is loading. Please wait.

Social Media And Global Computing Managing MVC with Model Validation

Similar presentations


Presentation on theme: "Social Media And Global Computing Managing MVC with Model Validation"— Presentation transcript:

1 Social Media And Global Computing Managing MVC with Model Validation

2 Validation Attributes
Common Validation Attributes for Model Properties (which optionally can use an ErrorMessage parameter): Range [Range(0,30)] [Range(0,30,ErrorMessage = "Must be in range from 0 to 30")] Required [Required()] [Required(ErrorMessage = "This field is required")] StringLength [StringLength(50)] [StringLength(50, MinimumLength=5, ErrorMessage = "Length must be between 5 and 50 characters")]

3 Validation Attributes
The Address Attribute is used to determine if an address has a valid format, which consist of three parts: the uname, sign, and the domain name. So is valid, but “john” is not valid, nor is Address [ Address] [ Address(ErrorMessage = "Invalid Address"]

4 Validation Attributes
The DisplayFormat Attribute is often also needed to deal with handling empty strings when they are allowed, because an empty string field can be incorrectly saved as a null value, which is different than an empty string value: DisplayFormat [DisplayFormat(ConvertEmptyStringToNull = false)]

5 Validation Attributes
In general, Validation Attributes are placed above Properties in program, and the DataAnnotations path needs to be declared: using System.ComponentModel.DataAnnotations; namespace MyProject.Models { public class Account [Required(ErrorMessage = "The ID Is Required")] public int ID { get; set; } [StringLength(50, ErrorMessage = "Names can’t be longer than 50 characters")] public string Name { get; set; } }

6 Validation Attributes
When the fields of a class come from a Database, a class is created for those fields already when you created an ADO.NET Data Entity Model. Now you need to create another one if you want to add special added Validation properties.

7 Validation Attributes
The special class is a Metadata class. It has the same name as the record in the Table, but it has the word Metadata after the name. You need to create a class with that name and then with the Metadata descriptor shown below: [MetadataType(typeof(ClassNameMetadata))] public partial class ClassName { }

8 Validation Attributes
This is what your new class would look like: using System.ComponentModel.DataAnnotations; namespace MyProject.Models { [MetadataType(typeof(ClassNameMetadata))] public partial class ClassName } public class ClassNameMetadata [Required(ErrorMessage = "The ID Is Required")] public int ID { get; set; } [StringLength(50, ErrorMessage = "Names can’t be longer than 50 characters")] public string Name { get; set; }

9 ValidationSummary You can show a general summary message concerning the validation errors using the ValidationSummary HTML Helper function: When ValidationSummary is given true - @Html.ValidationSummary(true) - only Model-Level errors are shown, but if it is given false, then both Model-Level and Property Errors are shown. Model-Level errors are created in action methods with: ViewBag.ModelState.AddModelError("","The Model-Level Error Message");

10 ValidationSummary and ValidationMessageFor
You can also give ValidationSummary a string message, and the message will show up above the Model-Level and Property-Level Errors, if there are any: @Html.ValidationSummary("There were some errors. Please correct them:“) The ValidationMessageFor HTML Helper function is used to determine which fields to manage the validation for: @Html.ValidationMessageFor(Function(model) model.ID)

11 Modifying A Field’s Label
You can modify a label for a field using the Display Attribute. For instance, if a field name is “StreetAddress” and you want it to show as “Street Address” (with a space), you would change its label like so: [Display(Name = “Street Address")] public string StreetAddress { get; set; })


Download ppt "Social Media And Global Computing Managing MVC with Model Validation"

Similar presentations


Ads by Google