我有一个简单的 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”会导致自动验证?