Daniel Santos Daniel Santos - 5 months ago 36
ASP.NET (C#) Question

ASP.NET MVC. Non required fields are required in validations

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

class MyClass{

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

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

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

but whenever I set the
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?


The TimeSpan is not nullable by default

Use Nullable;

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


public TimeSpan? t2 { get; set; }