使用HTML Helper将隐藏字段设置为Date Time Now

时间:2014-01-22 06:46:01

标签: asp.net-mvc date razor hiddenfield

我希望在ASP.NET MVC 5中做一些事情,将隐藏字段设置为当前日期和时间。类似的东西:

    @Html.HiddenFor(model => model.CommentTime, new { @Value=DateTime(Now) })

2 个答案:

答案 0 :(得分:4)

尝试像这样设置

 @Html.HiddenFor(model => model.CommentTime, new { @Value=System.DateTime.Now })

答案 1 :(得分:1)

在Controller中,创建模型时,只需设置属性:

var model = new MyModel
            {
                 CommentTime = DateTime.Now
            };

return View(model);