Eric Bishard
Eric Bishard

Reputation: 5341

Set hidden field using HTML Helper to Date Time Now

I'm looking to do something in ASP.NET MVC 5 to set a hidden field to current date and time. Something like:

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

Upvotes: 0

Views: 8716

Answers (2)

Ivo
Ivo

Reputation: 8352

In your Controller, when creating the model, just set the property:

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

return View(model);

Upvotes: 1

Nilesh Gajare
Nilesh Gajare

Reputation: 6398

Try to do set like this

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

Upvotes: 4

Related Questions