2013-12-17 45 views
1

在我的視圖中我有一個dropdownlistfor。下拉列表有5個選項。最後一個選項是「自定義」。當選擇這一個時,將顯示兩個文本框。MVC驗證RequiredIfTrue不起作用

我的模型:我查看

public class ReportModel 
{ 
    public DateType DateType { get; set; } 

    public List<TypeDTO> DateTypesDTO { get; set; } 

    [RequiredIfTrue("Custom", ErrorMessage = "Dit veld is verplicht")] 
    public DateTime? CustomDateFrom { get; set; } 

    [RequiredIfTrue("Custom", ErrorMessage = "Dit veld is verplicht")] 
    public DateTime? CustomDateTo { get; set; } 

    public bool Custom { get; set; } 
} 

部分:

<div class="control-group"> 
    <label class="control-label">Tijdsinterval</label> 
    <div class="controls"> 
     @Html.DropDownListFor(m => m.DateType, new SelectList(Model.DateTypesDTO, "DateType", "Translation"), new { id = "timespan", name = "timespan" }) 
    </div> 
</div> 
<div class="control-group hide" id="custom"> 
    <label class="control-label">Van</label> 
    <div class="controls controls-margin"> 
     <div class="input-append datetimepicker"> 
      @Html.TextBoxFor(m => m.CustomDateFrom, new { id = "datetimepicker", @class = "add-on", type = "text", data_format = "dd-MM-yyyy hh:mm" }) 
      @Html.ValidationMessageFor(m => m.CustomDateFrom) 
      @Html.HiddenFor(m => m.Custom); 
     </div> 
    </div> 
    <label class="control-label">Tot</label> 
    <div class="controls controls-margin"> 
     <div class="input-append datetimepicker"> 
      @Html.TextBoxFor(m => m.CustomDateTo, new { id = "datetimepicker", @class = "add-on", type = "text", data_format = "dd-MM-yyyy hh:mm" }) 
      @Html.ValidationMessageFor(m => m.CustomDateTo) 
     </div> 
    </div> 
</div> 
<script> 
    $(function() { 
     $('#partial-view').on('change', '#timespan', function() { 
      if ($(this).val() == 'Custom') { 
       $('#custom').show(); 
      } else { 
       $('#custom').hide(); 
      } 
     }); 
    }); 
</script> 

的問題是,在控制器if (ModelState.IsValid)始終是真實的,即使兩個文本框爲空。我究竟做錯了什麼?

+0

你在哪裏設置「自定義」爲真? – Dave

+0

我不這樣做了。 我在onchange函數中首先在JQuery中做了它,但後來我得到一個'Model' = null的異常。 我不知道該在哪裏做。 – Jeffrey

+0

'if($(this).val()=='Custom'){$('#custom')。show(); @ Model.Custom = true; }' 'else {$('#custom')。hide(); @ Model.Custom = false; }' – Jeffrey

回答

0

我已經試過幾乎所有的東西,但它的作品吧!

的型號:查看的

public class ReportModel 
{ 
    public DateType DateType { get; set; } 

    [Required(ErrorMessage = "Dit veld is verplicht")] 
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] 
    public DateTime CustomDateFrom { get; set; } 

    [Required(ErrorMessage = "Dit veld is verplicht")] 
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] 
    public DateTime CustomDateTo { get; set; } 

    public List<TypeDTO> DateTypesDTO { get; set; } 
} 

部分:

<div class="control-group"> 
    <label class="control-label">Tijdsinterval</label> 
    <div class="controls"> 
     @Html.DropDownListFor(m => m.DateType, new SelectList(Model.DateTypesDTO, "DateType", "Translation"), new { id = "timespan", name = "timespan" }) 
    </div> 
</div> 
<div class="control-group hide" id="custom"> 
    <label class="control-label">Van</label> 
    <div class="controls controls-margin datetimepicker"> 
      @Html.TextBoxFor(m => m.CustomDateFrom, "{0:dd-MM-yyyy}", new { @class = "add-on", data_format = "dd-MM-yyyy" }) 
      @Html.ValidationMessageFor(m => m.CustomDateFrom) 
    </div> 
    <label class="control-label">Tot</label> 
    <div class="controls controls-margin datetimepicker"> 
      @Html.TextBoxFor(m => m.CustomDateTo, "{0:dd-MM-yyyy}", new { @class = "add-on", data_format = "dd-MM-yyyy" }) 
      @Html.ValidationMessageFor(m => m.CustomDateTo) 
    </div> 
</div> 
<script> 
    $(function() { 
     $('#partial-view').on('change', '#timespan', function() { 
      if ($(this).val() == 'Custom') { 
       $('#custom').show(); 
      } else { 
       $('#custom').hide(); 
      } 
     }); 
    }); 
</script> 

的問題是這樣的:(見問題)

<div class="controls controls-margin"> 
     <div class="input-append datetimepicker"> 
     </div> 
    </div> 

後,我解決了這個問題,我對日期時間的驗證有很多麻煩。問題是這行代碼:data_format = "dd-MM-yyyy hh:mm"。驗證器給出一個ErrorMessage:'字段CustomDateFrom必須是一個日期。'

現在我只發送的日期,而不是時間:data_format = "dd-MM-yyyy"

+0

如果您將dd-MM-yyyy更改爲dd/MM/yyyy,則應允許接受日期格式。我有類似的問題,這就是我解決它的方法。 – Scanner

1

如果Custom爲真,則表示這些字段是必需的。問題是沒有任何地方設置爲Custom,所以它將默認爲false,並且字段永遠不會被需要。所選的值將在DateType進行設置,這樣你就可以很容易地通過創建自定義的getter你Custom物業解決問題:

public bool Custom 
{ 
    get { return DateType == "Custom"; } 
}