我有一份埃德基观点,有一份选编清单和一对二的盒子,这些箱子为可取消的田地生产。 如果没有日期,就没有进行验证,这是正确的。
然而,如果我投入一个无效的日期,验证工作不会发现这一点,并进而采取行动。 我如何在日期领域进行验证?
我的模式:
public class CaseEmployer
{
public int Id { get; set; }
public int CaseId { get; set; }
public Company Company { get; set; }
[Display(Name = "Start Date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode=true)]
public DateTime? StartDate { get; set; }
[Display(Name = "End Date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? EndDate { get; set; }
public int? UserId { get; set; }
public DateTime? TimeStamp { get; set; }
}
我的看法:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Employer</legend>
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.CaseId)
<div class="editor-label">
@Html.Label("Company:")
</div>
<div class="editor-field">
<select name="companySelect" id="companySelect">
@foreach (var company in ViewBag.CompanyOptions)
{
<option value="@company.Key">@company.Value</option>
}
</select>
</div>
<div class="editor-label">
@Html.LabelFor(model => model.StartDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.StartDate)
@Html.ValidationMessageFor(model => model.StartDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EndDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EndDate)
@Html.ValidationMessageFor(model => model.EndDate)
</div>
<p>
<input type="submit" value="Update" />
<input type="button" value="Cancel" onclick="javascript:window.close()" />
<input type="button" value="Delete" onclick="javascript:confirmDeleteCaseEmployer()" />
</p>
</fieldset>
** 本文件迟交。 Edit**
I added "Required" attributes to the class to test if the validation would trigger, if I submitted the form without StartDate and EndDate, and the validation does trigger with those attributes.
谢谢!