如何使DateTime绑定到RazorPages ViewModel? [英] How to make DateTime bind to RazorPages ViewModel?

查看:111
本文介绍了如何使DateTime绑定到RazorPages ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用RazorPages的ASP Core 2.2项目,我绑定了一些数据,其中一些我发送给了客户端,客户端需要在发送之前将数据进行必要的编辑.

这一切都可以正常工作,直到我想绑定一个DateTime对象.数据按预期绑定到HTML datetime-local控件,并且根据网络流量,它以POST请求形式发送回数据,但是OnPost中的DateTime对象始终读为01/01/0001

我已经在其他浏览器中进行了检查,以防出现问题. FireFox甚至不能正确呈现本地日期时间(它只是一个文本框,而不是日期选择器).

我还尝试将DateTime对象作为本地时间和UTC时间发送,并且我将自己的区域性设置为en-GB,因为这就是我所在的位置.

我的代码如下:

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设置为:

StartAt = DateTime.Now.ToUniversalTime(),

并被绑定在这样的视图上:

<input asp-for="@Model.CreationData.DataLines[lnNo].StartAt" class="form-control"/>

整个绑定对象是:

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; }
    }
}

下面是显示HTML5控件,绑定对象结果无效以及在OnPost中发送的表单数据的屏幕截图.

如您所见,我显然希望DateTime正确绑定.

有什么想法吗?

解决方案

根据您的要求,我会将其写为答案.

应将字段StartAt转换为带有setter和getter的属性:

public DateTime StartAt { get; set; }


通过制作自定义模型活页夹.

假设您正在使用不同的DateTime格式和区域性,并且希望将所有值提交到后端时都采用特定的格式.您可以将它作为活页夹编写一次,而不必在任何地方手动完成,

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?

解决方案

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.

这篇关于如何使DateTime绑定到RazorPages ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆