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

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

问题描述

在这个文本框的MVC视图页面,加载,我想默认显示当前日期和时间。我怎样才能做到这一点?在剃刀。

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


解决方案

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

  myModel.ReturnDate = DateTime.Now()返回视图(基于myModel)

您的观点是不是这样的控制器是这样做的更好的地方,在属性值设置正确的地方。

您甚至可以拥有它,这样对 ReturnDate 吸气返回当前的日期/时间。

 私人的DateTime _returnDate = DateTime.MinValue;
公众的DateTime ReturnDate {
   得到{
     回报(_returnDate == DateTime.MinValue)? DateTime.Now():_returnDate;
   }
   集合{_returnDate =值;}
}

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)

解决方案

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.

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天全站免登陆