0

I'm trying to make an EditorForwhere 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!

5
  • 1
    Try changing value=DateTime.Now to Value=DateTime.Now Commented Dec 14, 2016 at 18:21
  • HtmlHelper methods bind to your model. Set the value of Opgevoerd in the GET method and return the model to the view. - model.Opgevoerd = DateTime.Now; return View(model); Do NOT attempt to set the value attribute. Commented Dec 14, 2016 at 23:04
  • Thanks @BrianAlltop but that didn't work. Commented Dec 15, 2016 at 9:45
  • Thanks @StephenMuecke but that didn't work either x) Commented Dec 15, 2016 at 9:45
  • Of course it does. Edit you question to add the new code you have tried so we can fix your mistake Commented Dec 15, 2016 at 9:46

1 Answer 1

2

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 :)

Sign up to request clarification or add additional context in comments.

3 Comments

Seriously? [DisplayFormat(DataFormatString="{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public DateTime Opgevoerd { get; set; }, set the value to DateTime.Today; in the GET method before you pass the model to the view and @Html.EditorFor(model => model.Opgevoerd, new { htmlAttributes = new { @class = "form-control" }) (and why are you setting type="DateTime" when you only want a date component)
I set datetime because in the database it's set as datetime (I didnt make the data base) but my colleague only wanted the date. I'm relatively new to asp.net since I'm an intern so I didnt exactly know what you meant, I asked my supervisor and he told me to use javascript since opgevoerd isn't a model. it's an item inside a model. I'm sorry that I didn't do it your way.
A database (or model) DataTime type has absolutely nothing to do with the HTML-5 type="datetime" - its for generating the browsers HTML-5 datetimepicker (which only has limited browser support). Not only do you not need javascript - its stupid to do so (consider what happens when the use enters a value but you need to return the view - what the user typed in will be lost). And I know Opgevoerd is a property in your model - it would not work if it wasn't :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.