我有一个模特:
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,我猜它的一些变化会导致这个问题。
答案 0 :(得分:3)
这是MVC2的变化 - on automatic databinding, it's going to read all your properties (even the read-only ones) to try to validate them。
答案 1 :(得分:2)
将字段上的Required属性设置为false。默认情况下是真的。