我們正在創建幾個輸入字段,其中將包含一個開始日期和結束日期時間,每個字段中包含一個日期時間。我們驗證了當前正在工作,因此如果用戶在結束時間之後放置開始時間(並且在開始之前結束時間反之),它會顯示錯誤消息,但我們遇到了一個有趣的問題。MVC驗證 - 更新另一個字段?
例如,如果用戶在開始時間之前放置了結束時間,我們會在結束時間收到錯誤消息,如預期的那樣。
然而,如果開始日期然後修正,而不是結束日期,消息依然存在。
有沒有一種方法,以消除對不同字段/更新/改正驗證消息?下面是驗證碼:
[Required(ErrorMessage = "{0} is required!")]
[Display(Name = "Start Date")]
[Remote("IsTestPlanEndCorrect", "Analysis", AdditionalFields = "EndDate", HttpMethod = "Post", ErrorMessage = "Test Plan Start Time must be before End.")]
public System.DateTime StartDate { get; set; }
[Required(ErrorMessage = "{0} is required!")]
[Display(Name = "End Date")]
[Remote("IsTestPlanEndCorrect", "Analysis", AdditionalFields = "StartDate", HttpMethod = "Post", ErrorMessage = "Test Plan End Time must be after Start.")]
public System.DateTime EndDate { get; set; }
IsTestPlanEndCorrect
僅僅是返回如果傳遞的數據通過驗證true或false根據的方法。
編輯:爲字段 剃刀代碼:控制器
<div class="form-group">
@Html.LabelFor(t => t.StartDate, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(t => t.StartDate, new { @class = "form-control datetimepicker", placeholder = "Enter Test Plan Start"})
@Html.ValidationMessageFor(t => t.StartDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(t => t.EndDate, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(t => t.EndDate, new { @class = "form-control datetimepicker", placeholder = "Enter Test Plan End"})
@Html.ValidationMessageFor(t => t.EndDate)
</div>
</div>
輸入驗證碼:
[HttpPost]
public JsonResult IsTestPlanEndCorrect(DateTime EndDate, DateTime StartDate)
{
return Json(TimeManipulation.checkDate(StartDate,EndDate));
}
什麼是您的驗證代碼''IsTestPlanEndCorrect呢? – Shyju
'分析'是具有'IsTestPlanEndCorrect'方法的控制器的名稱 – Novastorm
是的。你可以分享這些代碼嗎?當你調試開始日期時,是否進行異步調用? – Shyju