Error verification Information (errormessage) for capturing views in ASP.

Source: Internet
Author: User

ViewModel class:

    <summary>///    reviews with validation view///    </summary> public    partial class vcreateshopcomment    {        [ Required (allowemptystrings = false, errormessage = "need to be")] public        string WW {get; set;}        [Required (allowemptystrings = false, ErrorMessage = "must comment")]        [Stringlength (errormessage = "comment cannot exceed {0} words")]        public string Commenttext {get; set;}        [Range (1, 5, errormessage = "to be scored between {1} and {2}")]        Public short score {get; set;}        [Range (0, Int. MaxValue, errormessage = "Your account is invalid")] public        int id{get; set;}    }

Controller

[Httppost]public actionresult Add (vcreateshopcomment v)        {   if (modelstate.isvalid)            {              //Love doing things            }            StringBuilder sb = new StringBuilder ();            var errors = modelstate.values;            foreach (var item in errors)            {                foreach (var item2 in item). Errors)                {
if (!item2. Errormessage.isnullorwhitespace ()) {sb. Appendline (item2.                   ErrorMessage + "<br/>"); }
}} return Content (sb.) ToString ()); }

The output of the error is traversed 2 times, provided that errormessage is needed for each validation attribute

Error verification Information (errormessage) for capturing views in ASP.

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.