ASP.NET MVC Razor - 所有表单字段都是必需的?

Chi*_*rin 5 forms asp.net-mvc jquery-validate required razor

我有一个由ASP.NET生成的表单.我有一些必填字段,我正在使用[Required] dataAnnotation.但是,根据我的网页,还需要没有[Required] DataAnnotation的元素.这些都不是必需的,但如果表格是空的,我就无法提交表格.

我使用scaffolding制作页面,使用jquery验证器(默认情况下)进行验证.

模型类(为清晰起见,省略了一些字段)

public class Room
{
    [Key]
    public int ID { get; set; }


    [Required(ErrorMessage = "Please enter the minimum (default) price for this room.")]
    [DataType(DataType.Currency)]
    [Display(Name = "Minimum price")]
    public decimal MinPrice { get; set; }

    [Display(Name = "Alternative price")]
    [DataType(DataType.Currency)]
    public decimal AltPrice { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在de .cshtml文件中创建表单字段的代码:

    <div class="form-group">
        @Html.LabelFor(model => model.MinPrice, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.MinPrice)
            @Html.ValidationMessageFor(model => model.MinPrice)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.AltPrice, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.AltPrice)
            @Html.ValidationMessageFor(model => model.AltPrice)
        </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

必填字段正确显示定义的错误消息(因此它会读取注释).非必填字段显示通用错误消息("需要替代价格字段".).

我搜索了很多,但到处都说如果[Required] DataAnnotation不在那里,表单中就不需要了.

Mar*_*llo 11

使非必填字段可为空.