当前日期和时间 - MVC 剃刀中的默认值 [英] Current date and time - Default in MVC razor

查看:20
本文介绍了当前日期和时间 - MVC 剃刀中的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当带有此文本框的 MVC 视图页面加载时,我希望默认显示当前日期和时间.我怎样才能做到这一点?用剃刀.

When the MVC view page with this textbox, loads , I would like to display current date and time by default. How can I do this? in razor.

  @Html.EditorFor(model => model.ReturnDate)

推荐答案

在从控制器返回模型之前,将 ReturnDate 属性设置为 DateTime.Now()

Before you return your model from the controller, set your ReturnDate property to DateTime.Now()

myModel.ReturnDate = DateTime.Now()

return View(myModel)

您的视图不是在属性上设置值的正确位置,因此控制器是更好的位置.

Your view is not the right place to set values on properties so the controller is the better place for this.

你甚至可以让 ReturnDate 上的 getter 返回当前日期/时间.

You could even have it so that the getter on ReturnDate returns the current date/time.

private DateTime _returnDate = DateTime.MinValue;
public DateTime ReturnDate{
   get{
     return (_returnDate == DateTime.MinValue)? DateTime.Now() : _returnDate;
   }
   set{_returnDate = value;}
}

这篇关于当前日期和时间 - MVC 剃刀中的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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