私は単純なViewModelを持っています
public class ProductViewModel
{
[Required(ErrorMessage = "This title field is required")]
public string Title { get; set; }
public double Price { get; set; }
}
これがこのビューモデルに基づく私のフォームです。
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>ProductViewModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
価格フィールドを検証したくありません。何も入力されていない場合は、自動的に検証され、このフィールドが必須であることが表示されます。価格に double を使用していることに気付きました。「文字列」に変更した場合。検証が削除されます。「double」と入力すると自動検証が発生するのはなぜですか?