如何在ASP.NET Core 2.0,RAZOR标记助手中设置日期,时间,数字的格式 [英] How to format Date, Time, Number in ASP.NET Core 2.0, RAZOR Tag Helpers

查看:387
本文介绍了如何在ASP.NET Core 2.0,RAZOR标记助手中设置日期,时间,数字的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用Razor标记助手在ASP.NET MVC Core 2.0应用程序中格式化日期时间和数字时遇到问题.

I'm facing problems trying to format DateTime and Numbers in ASP.NET MVC Core 2.0 App using Razor Tag Helpers.

遵循良好实践,我想在与表单视图相关的数据模型中指定数据格式.

Following the good practices I would like to specify data format at the data model related to the form view.

我的模特

public class OrderFormModel
{
    public bool IsEdit { get; set; }

    [Display(Name = "Order Time: ")]
    public DateTime ORDER_LOCAL_TIME { get; set; }

    [Display(Name = "Order Qty (K): ")]
    public int QUANTITY_K { get; set; }

视图:

        <div class="form-group col-md-9">
            <label asp-for="ORDER_LOCAL_TIME"></label>
            <input asp-for="ORDER_LOCAL_TIME" class="form-control" />
            <span asp-validation-for="ORDER_LOCAL_TIME" class="text-danger"></span>
        </div>
        <div class="form-group col-md-6">
            <label asp-for="QUANTITY_K"></label>
            <input asp-for="QUANTITY_K" min="1" class="form-control" style="text-align:right" />
            <span asp-validation-for="QUANTITY_K" class="text-danger"></span>
        </div>

所需的格式是

12/12/2017 12:23 (can be separated in to two fields as workaround)

1 000.145

1 000.145

我一直在尝试很多选项,但是它们都不起作用. 对于数字输入,我希望当用户输入值并留下输入时,要应用的格式.

I've been trying a lot of options but none of them works. For the number input, I would like when the user enters the value and leaves the input, the format to be applied.

我一直在尝试:

 [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm}")]
  (not applied at all)

 [DisplayFormat(DataFormatString = "{0:N3}")]
  (not applied at all)

 [DataType(DataType.Time)]
  (applied but seconds, milliseconds and AM/PM present)

 [DataType(DataType.Date)]
  (works for date only field)

我们非常感谢您的帮助.谢谢.

Your help is highly appreciated. Thanks.

已更新

正如Stephen Muecke指出的,[DisplayFormat]将在首次呈现页面时应用该格式.同意,谢谢. 我具有ORDER_LOCAL_TIME和QUANTITY_K属性的初始值. DateTime =现在(对于ORDER_LOCAL_TIME)和1000(对于QUANTITY_K).

As Stephen Muecke noted [DisplayFormat] applies the format when the page is first rendered. Agree, thanks. I have initial values for ORDER_LOCAL_TIME and QUANTITY_K properties. DateTime = now for ORDER_LOCAL_TIME and 1000 for QUANTITY_K.

因此,例如,在新订单操作"中,我希望表单视图"显示带日期的日期(仅限mm:ss)和数量(带千个分隔符)的格式化初始值.问题是我无法实现.

So for example on New Order Action I would like Form View to show formatted initial values for Date with Time (mm:ss only) and Quantity (with thousand separators). The problem is I can't achieve that.

正如Stephen Muecke注意到的那样,当您在浏览器中并且用户编辑一个值并保留输入内容时所应用的格式是另一回事. 如果我使用连接到QUANTITY输入的OnChange事件的Java Script函数,则可以格式化并尝试将格式化的数字作为文本返回到输入.但在这种情况下:<< jquery.js:7592指定的值"3000"不是有效数字.该值必须与以下正则表达式匹配:-?(\ d + | \ d +.\ d + |.\ d +)([eE] [-+]?\ d +)? >>

As Stephen Muecke notices format applied when your in the browser and the user edits a value and leaves the input is another story. If I use Java Script function connected to the OnChange event of the QUANTITY input, I can format and try to return formatted number as text to the input. But in this case: << jquery.js:7592 The specified value "3 000" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)? >>

推荐答案

我正在ASP.NET Core中工作,并且找到了解决该问题的方法,这个问题困扰了我一个星期(但并没有解决我的问题).我在这里或其他地方找到的任何论坛中都感到满意.)

I'm working in ASP.NET Core and have found a solution to this problem that puzzled me off and on for a week (and was not resolved to my satisfaction in any of the forums I found here or elsewhere.)

我不想在模型或控制器中编辑属性,因为我希望将整个日期(带有时间戳)存储在数据库中.

I did not want to edit the property in the model or in the controller because I wanted the whole date (with timestamp) to be stored in the database.

解决方案是在视图中传递@item.CreatedDate.Date.ToShortDateString(),现在在希望显示有限日期信息的页面上传递我想要的格式MM/DD/YYYY. ("CreatedDate"是我的财产).希望能消除一些头痛.

The solution was to pass @item.CreatedDate.Date.ToShortDateString() in the view, now my desired format MM/DD/YYYY on the page where I want limited date information displayed. ("CreatedDate" is my property). Hope that clears up some headaches.

这篇关于如何在ASP.NET Core 2.0,RAZOR标记助手中设置日期,时间,数字的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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