C# MVC验证要求真实无';行不通

C# MVC验证要求真实无';行不通,c#,asp.net,asp.net-mvc,validation,C#,Asp.net,Asp.net Mvc,Validation,在我看来,我有一个下拉列表。下拉列表有5个选项。最后一个选项是“自定义”。选择此选项后,将显示两个textboxfor 我的模型: public class ReportModel { public DateType DateType { get; set; } public List<TypeDTO> DateTypesDTO { get; set; } [RequiredIfTrue("Custom", ErrorMessage = "Dit veld

在我看来,我有一个下拉列表。下拉列表有5个选项。最后一个选项是“自定义”。选择此选项后,将显示两个textboxfor

我的模型:

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; }
}
公共类报表模型
{
公共日期类型日期类型{get;set;}
公共列表DateTypesTo{get;set;}
[RequiredIfTrue(“自定义”,ErrorMessage=“Dit veld is verplicht”)]
公共日期时间?CustomDateFrom{get;set;}
[RequiredIfTrue(“自定义”,ErrorMessage=“Dit veld is verplicht”)]
公共日期时间?CustomDateTo{get;set;}
公共布尔自定义{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>

蒂辛特瓦尔酒店
@DropDownListFor(m=>m.DateType,new SelectList(Model.datetypesdo,“DateType”,“Translation”),new{id=“timespan”,name=“timespan”})
厢式货车
@Html.TextBoxFor(m=>m.CustomDateFrom,新的{id=“datetimepicker”,@class=“add-on”,type=“text”,data_format=“dd-MM-yyyy-hh:MM”})
@Html.ValidationMessageFor(m=>m.CustomDateFrom)
@HiddenFor(m=>m.Custom);
托特
@Html.TextBoxFor(m=>m.CustomDateTo,新的{id=“datetimepicker”,@class=“add-on”,type=“text”,data_format=“dd-MM-yyyy-hh:MM”})
@Html.ValidationMessageFor(m=>m.CustomDateTo)
$(函数(){
$(“#局部视图”)。关于('change','#timespan',函数(){
if($(this).val()=='Custom'){
$(“#自定义”).show();
}否则{
$(“#自定义”).hide();
}
});
});

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

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

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

我几乎什么都试过了,但它现在起作用了

模型:

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; }
}
公共类报表模型
{
公共日期类型日期类型{get;set;}
[必需(ErrorMessage=“Dit veld is verplicht”)]
[DisplayFormat(DataFormatString=“{0:dd-MM-yyyy}”,ApplyFormatInEditMode=true)]
public DateTime CustomDateFrom{get;set;}
[必需(ErrorMessage=“Dit veld is verplicht”)]
[DisplayFormat(DataFormatString=“{0:dd-MM-yyyy}”,ApplyFormatInEditMode=true)]
public DateTime CustomDateTo{get;set;}
公共列表DateTypesTo{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>

蒂辛特瓦尔酒店
@DropDownListFor(m=>m.DateType,new SelectList(Model.datetypesdo,“DateType”,“Translation”),new{id=“timespan”,name=“timespan”})
厢式货车
@Html.TextBoxFor(m=>m.CustomDateFrom,“{0:dd-MM-yyyy}”,new{@class=“add-on”,data_format=“dd-MM-yyyy”})
@Html.ValidationMessageFor(m=>m.CustomDateFrom)
托特
@Html.TextBoxFor(m=>m.CustomDateTo,“{0:dd-MM-yyyy}”,新的{@class=“add-on”,data_format=“dd-MM-yyyy”})
@Html.ValidationMessageFor(m=>m.CustomDateTo)
$(函数(){
$(“#局部视图”)。关于('change','#timespan',函数(){
if($(this).val()=='Custom'){
$(“#自定义”).show();
}否则{
$(“#自定义”).hide();
}
});
});
问题是:(见问题)


解决这个问题后,我在验证日期时间方面遇到了很多麻烦。问题是这行代码:
data\u format=“dd-MM-yyy-hh:MM”
。验证器会给出一条
错误消息
:“CustomDateFrom字段必须是日期。”


现在我只发送日期而不发送小时数:
data\u format=“dd-MM-yyyy”

在哪里将“自定义”设置为true?我不再这样做了。我首先在onchange函数的JQuery中执行了此操作,但随后得到了一个异常,
模型
=null。我不知道在哪里做这个。
if($(this).val()='Custom'){$('#Custom').show();@Model.Custom=true;}
否则{$('#Custom').hide();@Model.Custom=false;}
您在ReportModel类中没有“DateType”属性。哦,对不起。我没有显示所有的代码,因为不是所有的代码都是相关的。我忘了显示日期类型。如果您将dd-MM-yyyy更改为dd/MM/yyyy,则应允许接受日期格式。我有一个类似的问题,我就是这样解决的。
    <div class="controls controls-margin">
        <div class="input-append datetimepicker">
        </div>
    </div>