1

I have the following two object Classes :

[Serializable]
[DataContract]
public class W_ListItemPrice
{
    [DataMember]
    [Required]
    public string nSize { get; set; }
    [DataMember]
    [Required, Range(0.5, Constants.FOOD_ITEM_MAX_SELLING_PRICE, ErrorMessage = "Price range invalid")]
    public decimal ItemPrice { get; set; }
}

[Serializable]
    [DataContract]
    public class W_Listing
    {
        [DataMember]
        [Required, RegularExpression(GlobalFromat.IDformat, ErrorMessage = "Invalid FoodID format")]
        public Int64 MemberID { get; set; }
        [DataMember]
        [Required, RegularExpression(GlobalFromat.IDformat, ErrorMessage = "Invalid ScheduleID format")]
        public Int64 ScheduleID { get; set; }
        [DataMember]
        [Required]
        public List<Days?> FoodDays { get; set; }
        [Required]
        public List<W_ListItemPrice> ListingPortionPrice { get; set; }

        public W_Listing()
        {
            oDays = new List<Days?>();
            ListingPrice = new List<W_ListItemPrice>();
        }
    }

When I call the function with Class W_Listing, it validates only the member items listed in W_Listing class. It does not validate members in W_ListItemPrice class ? It does not care what I pass or not.

Why ?

PCG
  • 2,049
  • 5
  • 24
  • 42
  • This two classes not inherited! – isaeid Apr 04 '19 at 05:07
  • you're using the term `inherited` incorrectly. you mean referenced or used – jazb Apr 04 '19 at 05:08
  • There isn't really a way for Data Annotations to apply to the elements of a list. You would have to do is create a wrapper class and apply the Data Annotation to the elements in the wrapper class. Look at this example: https://stackoverflow.com/questions/23337170/data-validation-for-every-item-in-a-list-of-my-viewmodel – Rahul Sharma Apr 04 '19 at 05:18
  • Yes, right term is referenced. I corrected my question, thanks for letting me know – PCG Apr 04 '19 at 05:38
  • Thank you Sharma. I think that's the right approach. – PCG Apr 04 '19 at 05:43
  • @Sharma, I could add element count attribute, but how I could use the W_listItemPrice validation attributes which are already setup without writting separate attributes for those elements ? – PCG Apr 04 '19 at 06:18
  • @PCG In your View you could call them like this: `@Html.TextBoxFor(model => model.ListingPortionPrice[0].ItemPrice )` – Rahul Sharma Apr 04 '19 at 07:38

0 Answers0