MVC验证RequiredIfTrue不起作用

时间:2013-12-17 13:41:37

标签: c# asp.net asp.net-mvc validation

在我的视图中,我有一个下拉列表。下拉列表有5个选项。最后一个选项是“自定义”。选择此项后,将显示两个文本框。

我的模特:

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>

问题在于,即使两个文本框为空,Controller if (ModelState.IsValid)也始终为true。我做错了什么?

2 个答案:

答案 0 :(得分:1)

如果Custom为真,您说这些字段是必需的。问题是,Custom每个地方都没有任何设置,因此它将默认为false,并且永远不需要这些字段。所选值将在DateType中设置,因此您可以通过为Custom属性创建自定义getter来轻松解决问题:

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

答案 1 :(得分: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"