In my Razor Pages .NET Core 3.1 application I have the following simple form
<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
is a date input field implemented using DateTimePicker jQuery plugin (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());
}
});
The output from console.log
above when I enter as date 13/02/2022 06:00
is the same: 13/02/2022 06:00
. So, I guess, this is the value submitted via POST. But on server-side I get "0001-01-01T00:00:00"
.
The code works fine when run in Debug mode from Visual Studio, or when I deploy it to a local web server. But when the app is deployed to production site using Docker the form submission doesn’t work. Submitted value is converted to "0001-01-01T00:00:00"
.
This is the Dockerfile
I am using:
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"]
For some reason when date is submitted it is not properly converted to a DateTime value by the model binder. Any ideas what I could do to solve the issue?
2
Answers
Comments from @Heretic Monkey helped me find a simple solution to my problem. I converted my form view model class from:
to:
So the value submitted is not converted to DateTime by the model binder. Instead I explicitly parse the string received using the client side date format.
Just rewrite this model like: [Make nullable by using ? ]
That’s it.