I have a simple ViewModel
public class ProductViewModel
{
[Required(ErrorMessage = "This title field is required")]
public string Title { get; set; }
public double Price { get; set; }
}
here's my form based on this View Model.
@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>
}
I dont want to validate the price field. but it got automatically validated, if nothing entered, will show this field is required. I notice I use double for price. If I changed it to "string". validation is removed. why type "double" cause automatic validation?