1

I do have a POCO Class with some non required fields:

class MyClass{

    [DataType(DataType.Time)]
    [Display(Name = "1st interval")]
    public TimeSpan t1{ get; set; }

    [DataType(DataType.Time)]
    [Display(Name = "2nd interval")]
    public TimeSpan t2 { get; set; }

    [DataType(DataType.Time)]
    [Display(Name = "3rd interval")]
    public TimeSpan t3 { get; set; }
}

but whenever I set the [Required] annotation or not. the validations fails. I always got the "2nd interval is required" message on my view.

I'm using only Server validation.

How can I solve it?

Daniel Santos
  • 14,328
  • 21
  • 91
  • 174
  • 3
    Make the properties nullable - `public TimeSpan? t2 { get; set; }` –  Sep 29 '16 at 03:19
  • There are 2 ways: using nullable properties or use `[Bind(Exclude)]`. Similar problem: http://stackoverflow.com/questions/2142990/the-id-field-is-required-validation-message-on-create-id-not-set-to-required – Tetsuya Yamamoto Sep 29 '16 at 03:48

1 Answers1

3

The TimeSpan is not nullable by default

Use Nullable;

public Nullable<TimeSpan> t2 { get; set; }

or

public TimeSpan? t2 { get; set; }
Daniel Santos
  • 14,328
  • 21
  • 91
  • 174
erdi yılmaz
  • 352
  • 1
  • 4
  • 15