視圖將顯示名稱或日期的搜索。問題在於ValidationSummary(根據類)需要輸入兩個值,並且會爲未顯示的字段/屬性添加錯誤。有沒有什麼辦法可以防止它在搜索只是提示姓名時需要日期?像禁用相關的數據註解一樣?是否可以在運行時繞過類的數據註釋?
注意:爲簡單起見,代碼已縮短,僅用於示例。請不要投票,如果你找不到「;」或類似的。
public class CustomSearch
{
[Required(ErrorMessage = "The text must be filled in.")]
public string SearchTextValue { get; set; }
[Required(ErrorMessage = "The Date must be selected.")]
public string SearchDateValue { get; set; }
}
<div>
<div>
@if (Model.SearchValidationSummary) { @Html.ValidationSummary(false) }
<div>
@using (Html.BeginForm("", "", FormMethod.Post))
{
if (Model.SearchText) { @Html.TextBoxFor(m => m.SearchTextValue) }
else if (Model.SearchDate) { @Html.DateEditFor(m => m.SearchDateValue) }
}
</div>
,如果你找htat有圖案的'requiredif'。 –