我有一个表单,我只想验证某些字段。 我曾经使用data-val =" false"来禁用一些字段验证。 它在客户端工作正常。但是服务器并没有。
这是我的代码
<div class="col-lg-12">
<div class="col-lg-6">
<label>@Html.RadioButtonFor(Function(model) model.Sex, "M") Male
</label>
</div>
<div class="col-lg-6">
<label>@Html.RadioButtonFor(Function(model) model.Sex, "F") Female
</label>
</div>
@Html.ValidationMessageFor(Function(model) model.Sex, "", New With {.class = "text-danger"})
</div>
<div class="col-lg-12 ">
@Html.LabelFor(Function(model) model.DateOfJoin, htmlAttributes:=New With {.class = "col-lg-4 control-label"})
<div class="col-lg-8">
@Html.EditorFor(Function(model) model.DateOfJoin, New With {.htmlAttributes = New With {.class = "form-control datefield", .data_val = False}})
@*@Html.ValidationMessageFor(Function(model) model.DateOfJoin, "", New With {.class = "text-danger"})*@
</div>
</div>
我的模特是
<MetadataType(GetType(EmployeeMasterMetaData))> _
Partial Public Class EmployeeMaster
Public Overridable Property DepartmentMaster As DepartmentMaster
Public Overridable Property DesignationMaster As DesignationMaster
End Class
Public Class EmployeeMasterMetaData
<Required(ErrorMessage:="Sex is required")>
Public Property Sex As String
<Display(Name:="Date Of Join")>
Public Property DateOfJoin As Date
End Class
我不想验证DateOfJoin字段并为其清空值,可以保存。 怎么办呢?
答案 0 :(得分:1)
如果您希望日期字段可以为空,请设置您的日期,如下所示。
public DateTime? DateOfJoin { get; set; }
日期时间类型后面的问号标记将数据设置为可空。如果使用Database first方法,则允许相应的字段接受空值。