ASP.NET mvc: how to automatically populate a date field with today's date?

Suppose the model has a datetime data type. Thus, in the field "Empty field" you must specify the date and time.

Is there a way to populate this HTML field with today's date / date as the default value?

+4
source share
2 answers

Plain

<%: Html.TextBox("date", DateTime.Now.ToShortDateString()) %> 

Or use javascript to get the client browser date. Better yet, use jquery datepicker for a good user interface for picking dates. With it, you can also pre-specify the default date:

 /** Enable jquery UI datepickers **/ $(document).ready(function () { $(function () { $(".date-select").datepicker({ dateFormat: 'dd.mm.yy' }); $(".date-select").datepicker($.datepicker.regional['sl']); }); $(function () { $("#someDateField").datepicker('setDate', new Date()); }); }); 
+3
source
 model.SomeDateField = DateTime.Now(); return View(model); 
+4
source

Source: https://habr.com/ru/post/1336648/


All Articles