Disable save button when validation fails

前端 未结 3 604
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 08:54

As you can likely see from the title, I am about to ask something which has been asked many times before. But still, after reading all these other questions, I cannot find a

3条回答
  •  余生分开走
    2021-01-14 09:53

    This article http://www.asp.net/mvc/tutorials/older-versions/models-%28data%29/validating-with-the-idataerrorinfo-interface-cs moves the individual validation into the properties:

    public partial class Player : IDataErrorInfo
    {
        Dictionary _errorInfo;
    
        public Player()
        {
            _errorInfo = new Dictionary();
        }
    
        public bool CanSave { get { return _errorInfo.Count == 0; }
    
        public string this[string columnName]
        {
            get 
            { 
                return _errorInfo.ContainsKey(columnName) ? _errorInfo[columnName] : null;
            }
        }
    
        public string FirstName
        {
            get { return _firstName;}
            set
            {
                if (String.IsNullOrWhiteSpace(value))
                    _errorInfo.AddOrUpdate("FirstName", "Geef een voornaam in");
                else
                {
                    _errorInfo.Remove("FirstName");
                    _firstName = value;
                }
            }
        }
    }
    

    (you would have to handle the Dictionary AddOrUpdate extension method). This is similar to your error count idea.

提交回复
热议问题