问题
I have an ASP Core 2.2 project that uses RazorPages, I am binding some data, some of which I send to the client, the client needs to edit this data where required before sending it back.
This works for everything perfectly fine until I want to bind a DateTime object. The data is bound to the HTML datetime-local control as expected, and according to the network traffic, it is sent back in the POST requests form data, however the DateTime object in the OnPost always reads as 01/01/0001
I have already checked this in other browsers in case this was the issues. FireFox doesn't even render datetime-local correctly (its just a textbox, and not a date picker).
I have also tried sending the DateTime object as local and UTC time, and I have set my culture to en-GB as this is where I am.
My code for this is as follows:
RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions
{
SupportedCultures = new List<CultureInfo> { new CultureInfo("en-GB") },
SupportedUICultures = new List<CultureInfo> { new CultureInfo("en-GB") },
DefaultRequestCulture = new RequestCulture("en-GB")
};
app.UseRequestLocalization(localizationOptions);
StartAt is being set for each line as:
StartAt = DateTime.Now.ToUniversalTime(),
and is being bound on the view like this:
<input asp-for="@Model.CreationData.DataLines[lnNo].StartAt" class="form-control"/>
The entire bound object is:
public class JobCreationData
{
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public int SelectedJobWorkflow { get; set; }
public List<JobCreationDataLine> DataLines { get; set; } = new List<JobCreationDataLine>();
public class JobCreationDataLine
{
public int WorkflowVersionId { get; set; }
public string WorkflowVersionCode { get; set; }
public string WorkflowVersionDescription { get; set; }
public int SelectedWorkCenterId { get; set; }
public DateTime StartAt;
public List<JobCreationBusinessUnit> BusinessUnits { get; set; }
}
public class JobCreationBusinessUnit
{
public int WantedBusinessUnitId { get; set; }
public int BusinessUnitId { get; set; }
public string Alias { get; set; }
public bool IsRequired { get; set; }
public int SelectedBusinessUnitLineId { get; set; }
}
}
Below are screenshots that show the HTML5 control, the bound object result is invalid, and the form data that is being sent in the OnPost.


As you can see, I clearly expect the DateTime to bind correctly.
Any ideas?
回答1:
As you requested I will write it as an answer.
The field StartAt
should be converted to property with a setter and getter:
public DateTime StartAt { get; set; }
You can take your code even further by making a custom model binder.
Imagine that you are working with different DateTime
formats and cultures, and you want all your values to be in a specific format when they are submitted to the back-end. Rather than doing it manually everywhere, you can write it once as a binder and let it take care of that for you.
来源:https://stackoverflow.com/questions/53882985/how-to-make-datetime-bind-to-razorpages-viewmodel