ASP. NET MVC series: Model

Source: Internet
Author: User

1. Model Task

Model is responsible for obtaining data through databases, AD (Active Directory), Web service, and other means, as well as saving user-entered data to databases, AD, Web service, and so on.

  Model only focuses on effectively providing data access mechanisms, data format validation, business logic validation, and more.

2. Define Model Metadata

Metadata is used to define related properties of the data model, such as display name, data length, and data format validation. Use the dataannotations mechanism in System.ComponentModel.DataAnnotations to assist in defining the ASP. NET MVC data model.

The validation properties of the System.ComponentModel.DataAnnotations namespace include: Stringlength, Required, RegularExpression, and range.

  Example:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.ComponentModel.DataAnnotations;namespacelibing.portal.web.models{ Public classProduct { Public intProductID {Get;Set; }        [Required] [Stringlength ( -, errormessage="Product name Maximum length 100 characters")]         Public stringProductName {Get;Set; } [RegularExpression (@"^\d+$", errormessage ="the inventory quantity can only be numeric")] [Range (0, -, errormessage ="Stock Qty 0 to 100")]         Public intUnitsInStock {Get;Set; } }}

ASP. NET MVC series: Model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.