简体   繁体   中英

MVC Razor - Set default value for EditorFor as Today's Date

I'm trying to make an EditorFor where it's value will be today's date, but I can't seem to figure out why it's not working.

I've tried a couple things already, but none of them seem to work.

Attempt 1:

View:

@Html.EditorFor(model => model.Opgevoerd, new { htmlAttributes = new { @class = "form-control", type = "DateTime", placeholder = "dd/mm/yyyy", value=DateTime.Now } })

Attempt 2:

Controller:

Model.Opgevoerd = DateTime.Now;

return View(Computers___tabel);

View:

@Html.EditorFor(model => model.Opgevoerd,"{0:dd/mm/yyyy}", new { htmlAttributes = new { @class = "form-control", type = "DateTime", placeholder = "dd/mm/yyyy" } })

I don't remember attempt 3 x)

I've been stuck at this for quite a while and I can't seem to figure it out.

Thanks!

After completely giving up on razor, I decided to fill the value of my EditorFor using javascript.

by doing:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
$('#opgevoerd').val(dd + "/" + mm + "/" + yyyy);

Thanks for the help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM