Free validation collection items are not empty / empty

Im using free check with mvc4

In my model, I have a list:

public List<int> TransDrops { get; set; } 

in an im view that creates text fields for each item in the list.

I want to subsequently make sure that each field is completed. (not null / empty)

OrderDetailsViewModelValidator is a model validator, what do I need?

thank

+4
source share
1 answer

First, you must use the integer type nullable for the collection item, otherwise empty text fields will be bound to the value zero , which makes it impossible to distinguish empty text fields and is filled with zeros.

public List<int?> TransDrops { get; set; } 

validator ( Must):

RuleFor(model => model.TransDrops)
    .Must(collection => collection == null || collection.All(item => item.HasValue))
    .WithMessage("Please fill all items");

, NotEmpty() : , IEnumerable not null 1 .

+6

Source: https://habr.com/ru/post/1534450/


All Articles