提交的日期时间转换为“0001-01-01T00:00:00”
Submitted datetime is converted to "0001-01-01T00:00:00"
在我的 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
是使用DateTimePickerjQuery插件实现的日期输入框(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 值。我可以做些什么来解决这个问题?
@Heretic Monkey 的评论帮助我找到了解决问题的简单方法。我从以下位置转换了表单视图模型 class:
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。相反,我使用客户端日期格式显式解析接收到的字符串。
在我的 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
是使用DateTimePickerjQuery插件实现的日期输入框(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 值。我可以做些什么来解决这个问题?
@Heretic Monkey 的评论帮助我找到了解决问题的简单方法。我从以下位置转换了表单视图模型 class:
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。相反,我使用客户端日期格式显式解析接收到的字符串。