The model in MVC is more than just a data transfer person.

Source: Internet
Author: User
Tags add time

In the model use, many people back to the previous write three-tier architecture to use it, the model as the sender of data.

Like a common notation.

        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 is the most commonly encountered, I have seen more than the code generator is also generated. Many people use model as a staging area for data transfer and data.

In fact, 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 empty")]        [Display (Name = "Role ID")] public int Roleid {get; set;}        [Required (errormessage = "account name cannot be empty")]        [Display (name = "account name")] public string name {get; set;}        [Required (errormessage = "Password cannot be empty")]                [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 = "Mailbox")] public string Email {get; set;}        [Display (Name = "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 Login Time")] public DateTime lastlogintime {get; set;} [Display (Name = "Recent login IP")] public string Lastloginip {get; set;}                [Display (Name = "lock")] public bool Islock {get; set;} [Display (Name = "Super Administrator")] public bool Issuperuser {get; set;}

All of the things that have been written about MVC are known, and there are examples in the default project.

But a lot of people don't use it well, or they don't. In the controller, it is possible to determine whether the model verifies success Modelstate.isvalid.

But this is not good enough, sometimes some fields do not need to be verified, and some must be verified. This modelstate.remove ("validated field"); it is flexible to use, to ignore that is that.

It also comes with a method to remove the error, returning a single error. There is no need to output each (I use this to take out a single error, and then the JSON is transmitted to the client).

        <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)            {                //get first                //errors = dic. Keys.first<string> ();                Errors = dic. Values.first (t = t.errors.count > 0). Errors[0]. errormessage;            }            return errors;        }

The model in MVC is more than just a data transfer person.

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.