PageModel.Validate C# (CSharp) Method

Validate() public method

public Validate ( ) : PageModel,
return PageModel,
    public PageModel Validate()
    {
        Assert.IsTrue(Value != null);

        var rules = new Dictionary<string, string[]>
        {
            {"Id", new string[]{"required", "minlength:2", "maxlength:140"}},
            {"Title", new string[]{"required", "minlength:2", "maxlength:140"}},
        };

        this.ValidateValue(rules);
        return this;
    }

Usage Example

コード例 #1
0
        public static void ValidateToModelState(this PageModel model, object?objectToValidate = null)
        {
            model.CheckNotNull(nameof(model));

            var errors = objectToValidate != null?objectToValidate.Validate() : model.Validate();

            if (errors != null)
            {
                foreach (var item in errors)
                {
                    model.ModelState.AddModelError(item.MemberNames.ToString(), item.ErrorMessage);
                }
            }
        }