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.