Daniel Santos Daniel Santos - 1 year ago 81
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?

Answer Source

The TimeSpan is not nullable by default

Use Nullable;

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


public TimeSpan? t2 { get; set; }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download