开发者

ASP.net validation: why do my DateTime fields also flag up as required?

开发者 https://www.devze.com 2023-02-10 12:21 出处:网络
I\'m implementing validation in my web app... the problem is it seems to be over-validating?! The controller code looks like:

I'm implementing validation in my web app... the problem is it seems to be over-validating?!

The controller code looks like:

[HttpPost]
[Authentication]
public ActionResult Create([Bind(Exclude = "Id")] CaseInfo caseInfo)
{
    if (!ModelState.IsValid)
    {
        SetupViewData();
        return View();
    }

    _repository.Create(caseInfo);
    return RedirectToAction("List");
}

This is the CaseInfo implementation:

public class CaseInfo :IValidatableObject
{   
    public virtual Guid Id { get; set; }
    public virtual DateTime ReferralDate { get; set; }
    public virtual int Decision { get; set; }
    public virtual string Reason { get; set; }
    public virtual DateTime StartDate { get; set; }
    public virtual DateTime EndDate { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        throw new NotImplementedException();
    }
}

And my CaseInfoMap:

public sealed class CaseInfoMap : ClassMap<CaseInfo>
{
    public CaseInfoMap()
    {
        Id(x => x.Id).Not.Nullable();
        Map(x => x.ReferralDate);
        Map(x => x.Decision);
        Map(x => x.Reason);
        Map(x => x.StartDate);
        Map(x => x.EndDate);
    }
}

But when I run it up and submit the form with no values, I get the following validation errors:

# The ReferralDate field is required.
# The StartDate field is requir开发者_开发知识库ed.
# The EndDate field is required.

But I haven't specified that these should be required?!! And why aren't the decision and reason fields throwing similar validation errors?

Can anyone shed some light on this?

I'm using .NET 4, and MVC 2.


Could you change this:

public virtual DateTime ReferralDate { get; set; } 

to

public virtual DateTime? ReferralDate { get; set; } 

You need to use nullable type for DateTime.

If you want to do the same for all other dates change them:

public class CaseInfo :IValidatableObject {        
    public virtual Guid Id { get; set; }     
    public virtual DateTime? ReferralDate { get; set; }     
    public virtual int Decision { get; set; } 
    public virtual string Reason { get; set; }     
    public virtual DateTime? StartDate { get; set; }     
    public virtual DateTime? EndDate { get; set; }      
    public IEnumerable<ValidationResult>

Validate(ValidationContextvalidationContext) {throw new NotImplementedException();} }

Check the MSDN documentation on datetime: http://msdn.microsoft.com/en-us/library/system.datetime(v=VS.90).aspx

You will notice that the values have a range, which do not include null, hence your validator fails. You will want to use the datetime nullable type.


The DateTime struct is not nullable, so it is in-effect required. You should use a Nullable<DateTime> [or DateTime? for the shorthand] if it is nullable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号