简体   繁体   English

如何从隐藏表单输入到数据库 MySQL 在 ASP.NET MVC 中输入 DateTime Now?

[英]How to input DateTime Now in ASP.NET MVC from the hidden form input to database MySQL?

I want to input datetime now to record add item time.我想现在输入日期时间来记录添加项目时间。 I currently use an input form with a hidden type to insert datetime into the database table column but it fails.我目前使用具有隐藏类型的输入表单将日期时间插入数据库表列,但它失败了。 How do I input datetime now in realtime with the format yyyy-mm-dd hh: mm: ss time format 24 hours into the database?, for example 2019-10-23 16:30:49.现在如何以 yyyy-mm-dd hh:mm:ss 时间格式 24 小时格式将日期时间实时输入数据库?例如 2019-10-23 16:30:49。 I am using ASP.NET MVC with MySql Database.我正在使用 ASP.NET MVC 和 MySql 数据库。 Thank You谢谢你

<form class="form-horizontal" method="post" action="CreateItem">
    <input type="text" class="form-control" name="NameItem">
    <input type="hidden" class="form-control" name="AddTime" value="DateTimeNow">
</form>

It is not advisable to add datetime from client side, you should add it using server side.. so assign DateTime.Now to object in backend before inserting record to database不建议从客户端添加日期时间,您应该使用服务器端添加它。所以在将记录插入数据库之前,将 DateTime.Now 分配给后端的 object

why datetime should not be created using Client side?为什么不应使用客户端创建日期时间?

Assume users trying to access application from different countries which have different time zone, then datetime will not be accurate in database, so when try to generate report or check anything based on datetime orders, then it will lead to confusion.假设用户尝试访问来自不同国家、不同时区的应用程序,那么数据库中的日期时间将不准确,因此当尝试根据日期时间命令生成报告或检查任何内容时,会导致混乱。

so the solution is to add datetime to class object before inserting to database as the following所以解决方案是在插入数据库之前将日期时间添加到 class object 如下

 [HttpPost]
  public async Task<IActionResult> UpdateMethod([FromBody] MyClass model)
  mode.CreationDate =  DateTime.Now;
  //then insert record to database 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM