繁体   English   中英

模型属性验证问题

[英]Model attribute validation issue

我正在格式化DateTime? 字段,如dd/MM/yyyy ,当我提交表单时显示验证错误。

在此处输入图片说明

我不明白为什么会这样?

模型

[Display(Name = "Expected Ending Time")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? ExpectedEndingTime { get; set; }

HTML

@Html.TextBoxFor(x => x.Requsition.ExpectedEndingTime, new { @class = "form-control dataPickerField", id = "ExpectedEndingTimeDataPicker", @readonly = true })

@Html.ValidationMessageFor(x => x.Requsition.ExpectedEndingTime)



<script>
    $(function () {            
        $('#ExpectedEndingTimeDataPicker').datepicker({
            format: 'dd/mm/yyyy',
            autoclose: true           
        })
        .on('changeDate', function (ev) {
              //  do things;
    );
    });
</script>

我认为DataFormatString仅用于显示,而ModelBinder并不用于解析。 因此,您的服务器仍然使用来自web.config的Culture。

您可以在该日期格式中使用的配置中对特定区域性进行硬编码。

这里有一个答案,可以帮助您- https://stackoverflow.com/a/8035636/169635它使用的CurrentCulture解析IModelBinder的样本。 您可以指定自己的格式

没什么对我有用...

因此,我向模型添加了1个额外的字段,并将DateTimeString一样保留了所需的格式。

对于需要DateTime格式的地方,我还有另一个字段。

[Display(Name = "Expected Ending Time")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? ExpectedEndingTime { get; set; }


[Required]
[Display(Name = "Expected Ending Time")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public string ExpectedEndingTimeAsString { get; set; }  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM