How to validate data stored in a C# class
asmx, c#, validation
Solution
You may look at this webpage: http://odetocode.com/blogs/scott/archive/2011/06/29/manual-validation-with-data-annotations.aspx
Problem
I have the following class in a C# ASMX web service, not MVC or web forms project. ``` public class Hotel { public int HotelId {get;set;} public string Name {get;set;} public Room[] Room { get; set; } [Range(0, 12, ErrorMessage = "Rating must be between 1 and 5")] public int Rating {get;set:} } public class Room { [Required] public int RoomId {get;set;} [Required] [StringLength(175)] public string Name {get; set;} } ``` Using System.ComponentModel.DataAnnotations, as I able to valid like above ? If so how to I get the response back of the validation error ? Also when the service starts it I reads in a Json data object like below. ``` oHotelRequest = JsonConvert.DeserializeObject<Hotel>(sJson); HotelResponse oHotelResponse = BookingAPI.Hotel.Get(oHotelRequest); return JsonConvert.SerializeObject(oHotelResponse); ``` Or am I able to do the validation when de-serializing the object ?