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)
This question is related to
asp.net-mvc
asp.net-mvc-3
asp.net-mvc-2
razor
html-helper
If you want to display date time on view without model, just write this:
Date : @DateTime.Now
The output will be:
Date : 16-Aug-17 2:32:10 PM
Isn't this what default constructors are for?
class MyModel
{
public MyModel()
{
this.ReturnDate = DateTime.Now;
}
public date ReturnDate {get; set;};
}
You could initialize ReturnDate on the model before sending it to the view.
In the controller:
[HttpGet]
public ActionResult SomeAction()
{
var viewModel = new MyActionViewModel
{
ReturnDate = System.DateTime.Now
};
return View(viewModel);
}
Source: Stackoverflow.com