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?

2
  • 3
    Make the properties nullable - public TimeSpan? t2 { get; set; } Commented Sep 29, 2016 at 3:19
  • There are 2 ways: using nullable properties or use [Bind(Exclude)]. Similar problem: stackoverflow.com/questions/2142990/… Commented Sep 29, 2016 at 3:48

1 Answer 1

3

The TimeSpan is not nullable by default

Use Nullable;

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

or

public TimeSpan? t2 { get; set; }
Sign up to request clarification or add additional context in comments.

1 Comment

is it equivalent to public TimeSpan? t2 {get;set;} ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.