亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何驗證與其他字段相關的字段

如何驗證與其他字段相關的字段

C#
吃雞游戲 2023-09-09 16:17:17
上課了Report。我想要 makeComment和ReasonIdsrequired if 的值Score小于 4。我不能使用屬性驗證,因為你不能使用字段作為屬性參數。我如何在 ASP.NET MVC 核心應用程序中驗證這些字段?public class Report{    public int Score { get; set; }    public string Comment { get; set; }    public int[] ReasonIds { get; set; }}
查看完整描述

1 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

這應該會給你你正在尋找的東西。


public class Report : ValidationAttribute

    {

        public int Score { get; set; }

        public string Comment { get; set; }

        public int[] ReasonIds { get; set; }


        protected override ValidationResult IsValid(

        object value, ValidationContext validationContext)

        {

            if(Score < 4 && (string.IsNullOrEmpty(Comment) || ReasonIds.Count() < 1))

            {

                return new ValidationResult(GeScoreErrorMessage());

            }

            return ValidationResult.Success;

        }


        private string GeScoreErrorMessage()

        {

            return $"If Score < 4 Comment and Reasons must be provided";

        }

    }


查看完整回答
反對 回復 2023-09-09
  • 1 回答
  • 0 關注
  • 109 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號