Validation of Guid

前端 未结 8 874
忘了有多久
忘了有多久 2020-12-31 02:47

I have a strongly-typed view which has a DropDownListFor attribute on it.

Each item in the dropdown list is represented by a GUID.

What I\'m after is a way

相关标签:
8条回答
  • 2020-12-31 03:45

    You can validate the Guid if it contains default values - "00000000-0000-0000-0000-000000000000".

      if (model.Id == Guid.Empty)
      {
               // TODO: handle the error or do something else
      }
    
    0 讨论(0)
  • 2020-12-31 03:46

    Bad answer. See below...

    Using a regex to block empty / default Guid worked for me. Use in combination with 'Required'. I.e.

        [Required]
        [RegularExpression("^(?!(00000000-0000-0000-0000-000000000000)$)", ErrorMessage = "Cannot use default Guid")]
        public Guid Id { get; set; }
    

    EDIT: Actually this doesn't work. It rejects everything. I'll leave this answer so others don't bother trying it.

    0 讨论(0)
提交回复
热议问题