【问题标题】:MVC validation RequiredIfTrue doesn't workMVC 验证RequiredIfTrue 不起作用
【发布时间】:2013-12-17 13:41:36
【问题描述】:

在我看来,我有一个下拉列表。下拉列表有 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>

问题在于控制器中if (ModelState.IsValid) 始终为真,即使两个文本框为空。我做错了什么?

【问题讨论】:

  • 您在哪里将“自定义”设置为 true?
  • 我不再这样做了。我首先在 JQuery 中的 onchange 函数中执行此操作,但随后出现 Model = null 的异常。我不知道在哪里做这个。
  • if ($(this).val() == 'Custom') { $('#custom').show(); @Model.Custom = true; } else { $('#custom').hide(); @Model.Custom = false; }
  • ReportModel 类中没有“DateType”属性。
  • 对不起。我没有显示所有代码,因为并非所有代码都是相关的。我忘了显示 DateType。

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我几乎尝试了所有方法,但现在可以了!

    模型:

    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"

    【讨论】:

    • 如果您将 dd-MM-yyyy 更改为 dd/MM/yyyy,这应该允许接受日期格式。我有一个类似的问题,这就是我解决它的方法。
    猜你喜欢
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多