Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET mvc: How to automatic fill the date field with today's date?

Assume that a model has a datetime datatype. So in view there will be a blank field ask you to input datetime.

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

like image 728
user469652 Avatar asked Jan 23 '11 05:01

user469652


People also ask

How can set current date in Datepicker in MVC?

$(". date"). datepicker( "setDate", "@Model. Birthdate");


2 Answers

model.SomeDateField = DateTime.Now();

return View(model);
like image 127
Scott Avatar answered Nov 15 '22 05:11

Scott


Simple

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

Or use javascript to get the client's browser date. Better yet use jquery's datepicker for nice UI for selecting dates. With it you can also prepopulate 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());
    });
});
like image 20
mare Avatar answered Nov 15 '22 04:11

mare