2010-08-16 172 views
1

我已经有了一个模型:MVC2验证模型还没有验证属性的属性

public class OverdraftForm 
{ 
    public string CustomerId { get; set; } 
    public PaymentType PaymentType { get; set; } 

    public OverdraftType Type { get; set; } 

    public decimal PermanentAmount { get; set; } 
    public int ExpirationPeriod { get; set; } 

    public decimal OnetimeAmount { get; set; } 
    public DateTime ExpirationDate { get; set; } 
    public CreditType CreditType { get; set; } 
    public string Comment { get; set; } 
} 

而且我的行为是

public ActionResult CreateOverdraft(OverdraftForm form) 
{ 
    if (form.Type == OverdraftType.Permanent) 
     return CreatePermanentOverdraft(form); 
    return CreateOnetimeOverdraft(form); 
} 

的一点是,当我调试它,甚至在第一行动作ModelState.IsValid为false,它表示OnetimeAmount应该有一个值。我使用MVC2,我想对它的一些更改会导致此问题。

回答

2

将字段上的Required属性设置为false。默认情况下它是真的。

+0

对不起,一个愚蠢的问题。但我应该怎么做? [必需(false)]不会(也不应该)工作 – HiveHicks 2010-08-16 14:47:35

+1

试图使其可以为空: 公共小数? OnetimeAmount {get;组; } – akonsu 2010-08-16 15:15:45

+0

谢谢,它的工作原理。 – HiveHicks 2010-08-16 15:21:57