In MVC, Model is not only a data transmitter, but also an mvcmodel.

Source: Internet
Author: User
Tags add time

In MVC, Model is not only a data transmitter, but also an mvcmodel.

When using a Model, many people use it the same way as when writing a three-tier architecture, and use the Model as the data transmitter.

For example, common writing

        public int Id { get; set; }        public int RoleId { get; set; }        public string Name { get; set; }        public string Password { get; set; }                public string NickName { get; set; }               public string Avatar { get; set; }        public string Email { get; set; }        public string Phone { get; set; }        public string Qq { get; set; }        public DateTime AddTime { get; set; }                public DateTime LastLoginTime { get; set; }        public string LastLoginIp { get; set; }        public bool IsLock { get; set; }                public bool IsSuperUser { get; set; }

This writing method is the most common, and I have seen many code generators generate this way. Many people regard the model as a data transmitter and a temporary data zone.

In fact, the Model can also be used to verify data. Reference: using System. ComponentModel. DataAnnotations; space

[Display (Name = "ID")] public int Id {get; set;} [Required (ErrorMessage = "role ID cannot be blank")] [Display (Name = "role ID")] public int RoleId {get; set;} [Required (ErrorMessage = "account Name cannot be blank")] [Display (Name = "account Name")] public string Name {get; set;} [Required (ErrorMessage = "password cannot be blank")] [Display (Name = "Password")] public string Password {get; set;} [Display (Name = "NickName")] public string NickName {get; set ;} [Display (Name = "Avatar")] [DataType (DataType. text)] public string Avatar {get; set;} [Display (Name = "Email")] public string Email {get; set ;} [Display (Name = "Mobile Phone")] public string Phone {get; set;} [Display (Name = "Qq number")] public string Qq {get; set ;} [Display (Name = "add time")] public DateTime AddTime {get; set;} [Display (Name = "Last Logon Time")] public DateTime LastLoginTime {get; set;} [Display (Name = "recently logged on IP")] public string LastLoginIp {get; set;} [Display (Name = "locked")] public bool IsLock {get; set;} [Display (Name = "Super administrator")] public bool IsSuperUser {get; set ;}

All those who have written MVC will know about it. This is an example in the default project.

However, it is difficult for many people to use or not to use it. In the controller, you can determine whether the model has been successfully verified ModelState. IsValid.

But this is not good enough. Sometimes some fields do not need to be verified, and some must be verified. In this way, ModelState. Remove ("verified field"); can be used flexibly. You can ignore that.

In addition, a method for retrieving errors is provided, and a single error is returned. It is not necessary to output every one (I use this to retrieve a single error and transmit it to the client in json format ).

/// <Summary> /// get a single error message /// </summary> /// <param name = "dic"> </param> /// <returns> </returns> public static string GetModelErros (this ModelStateDictionary dic) {string errors = ""; if (! Dic. isValid) {// obtain the first one // errors = dic. keys. first <string> (); errors = dic. values. first (t => t. errors. count> 0 ). errors [0]. errorMessage;} return errors ;}

 

Related Article

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.