【问题标题】:Submitted datetime is converted to "0001-01-01T00:00:00"提交的日期时间转换为“0001-01-01T00:00:00”
【发布时间】:2022-07-25 14:45:18
【问题描述】:

在我的 Razor Pages .NET Core 3.1 应用程序中,我有以下简单的形式

<form method="post" id="formReport">
  <div class="form-group">
    <label asp-for="Parameters.From" class="control-label"></label>
    <input id="txtFrom" asp-for="Parameters.From" type="text" class="form-control" style="width:90%;" />
  </div>
  <button type="submit" class="btn btn-primary btn-sm" title="Show report">
    <i class="far fa-eye"></i> Show Report
  </button>
</form>

txtForm 是使用 DateTimePicker jQuery 插件 (https://xdsoft.net/jqplugins/datetimepicker/) 实现的日期输入字段。

var from = $('#txtFrom').datetimepicker({
            format: 'd/m/Y H:i',
            formatDate: 'Y/m/d',
            formatTime: 'H:i',
            defaultTime: '06:00',
            mask: '39/19/9999 29:59',
            monthChangeSpinner: true,
            onChangeDateTime: function (dp, $input) {
                console.log($input.val());
            }
       });

当我输入日期13/02/2022 06:00 时,上面console.log 的输出是相同的:13/02/2022 06:00。所以,我猜,这是通过 POST 提交的值。但在服务器端我得到"0001-01-01T00:00:00"

当从 Visual Studio 在调试模式下运行时,或者当我将其部署到本地 Web 服务器时,代码运行良好。但是,当使用 Docker 将应用程序部署到生产站点时,表单提交不起作用。提交的值转换为"0001-01-01T00:00:00"

这是我正在使用的Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:3.1-bionic-arm64v8 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:3.1-bionic-arm64v8 AS build
WORKDIR /src
COPY ["myApp.Web/myApp.Web.csproj", "myApp.Web/"]
RUN dotnet restore "myApp.Web/myApp.Web.csproj"
COPY . .
WORKDIR "/src/myApp.Web"
RUN dotnet build "myApp.Web.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "myApp.Web.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

# Create a folder for saving images; this folder exists in container filesystem
# and has to be mounted to folder of the host filesystem inside docker-compose
RUN mkdir -p /app/images

ENTRYPOINT ["dotnet", "myApp.Web.dll"]

由于某种原因,在提交日期时,模型绑定程序未将其正确转换为 DateTime 值。有什么想法可以解决这个问题吗?

【问题讨论】:

  • JS 很可能无法识别日期格式。请注意,Date() 对象构造函数只会解析 mm/dd/yyyyyyyy/mm/dd 字符串格式。 Demo 这个在行动
  • @RoryMcCrossan 但代码在部署到本地服务器或从 Visual Studio 以调试模式运行时有效。在这些情况下,我在服务器端获得了正确的 DateTime 值。
  • 这意味着日期时间格式正在被服务器的文化设置更改。向客户端发送值时需要显式设置格式。
  • @RoryMcCrossan 既然我使用的是 Docker,我该怎么做呢?
  • 你在哪里设置txtFrom的值:yourDate.ToString("yyyy-MM-dd")。同样,我只是从您描述的行为中猜测,因为您没有显示任何相关代码,或者确认两个服务器之间的日期格式差异,因为这将是第一个逻辑调试步骤

标签: c# jquery docker docker-compose asp.net-core-3.1


【解决方案1】:

@Heretic Monkey 的评论帮助我找到了解决问题的简单方法。我从以下位置转换了我的表单视图模型类:

public class ReportFormViewModel
{
    public DateTime From { get; set; }
    public DateTime To { get; set; }       
}

到:

public class ReportFormViewModel
{
    public string From { get; set; }
    public string To { get; set; }

    public DateTime DateFrom
    {
        get
        {
            return DateTime.ParseExact(From, "dd/MM/yyyy HH:mm", 
                                         CultureInfo.InvariantCulture);
        }
    }

    public DateTime DateTo
    {
        get
        {
            return DateTime.ParseExact(To, "dd/MM/yyyy HH:mm",
                                         CultureInfo.InvariantCulture);
        }
    }

}

因此,模型绑定器不会将提交的值转换为 DateTime。相反,我使用客户端日期格式显式解析接收到的字符串。

【讨论】:

    【解决方案2】:

    只需重写这个模型:[Make nullable by using ?]

    public class ReportFormViewModel
    {
        public DateTime? From { get; set; }
        public DateTime? To { get; set; }       
    }
    

    就是这样。

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 2020-03-04
      • 2019-06-16
      相关资源
      最近更新 更多