シンプルなカスタム検証、MVC3シンプルなカスタム検証
私のモデルとカスタム検証:
public class Registration
{
[Required(ErrorMessage = "Date of Birth is required")]
[AgeV(18,ErrorMessage="You are not old enough to register")]
public DateTime DateOfBirth { set; get; }
}
public class AgeVAttribute : ValidationAttribute
{
private int _maxAge;
public AgeVAttribute(int maxAge)
{
_maxAge = maxAge;
}
public override bool IsValid(object value)
{
return false; <--- **this never gets executed.... what am I missing?**
}
}
(上記のインラインコメントを参照してください)
ビュー:
@using (Html.BeginForm()) {
@Html.ValidationSummary("Errors")
<fieldset>
<legend>Registration</legend>
<div class="editor-label">
@Html.LabelFor(model => model.DateOfBirth)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DateOfBirth)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
モデルを受信するコントローラはどのように見えますか? –
私は空のMVCプロジェクトであなたのコードを試して、IsValidの呼び出しが必要なように起こります。 – Iridio
'登録'タイプのモデルが使用されている場合、 'アクション'メソッドを提供しますか?私はあなたのコードをテストしました、それはサーバー側で働いています。 'AgeVAttribute'はIClientValidatableを実装していないので、クライアント側の検証はオフになっています。 – Kibria