0

So I'm trying to use a datepicker from http://www.eyecon.ro/bootstrap-datepicker/

I'm having the problem to set a default value for this datepicker. It shows me the format "0001-01-01 00:00:00" before I can set a date. What I want to do is to show todays date! I create the DOM as follows, which isn't a problem..

<div>
      @Html.EditorFor(model => model.ValidationValidFrom, new { htmlAttributes = new { @class = "form-control", @id = "ValidationValidFrom" }})
</div>

This creates the input with the id "ValidationValidFrom". And my jQuery code for the datepicker is as follows:

$("#ValidationValidFrom").datepicker({
        format: 'yyyy-mm-dd',
        defaultDate: new Date()
});

The only thing that works is the attribute format, the defaultDate doesn't work for some reason, I have tried other attributes shown in other threads, but to no avail..

The generated code looks like this:

<input class="form-control text-box single-line" data-val="true" data-val-date="The field ValidationValidFrom must be a date." data-val-required="The ValidationValidFrom field is required." id="ValidationValidFrom" name="ValidationValidFrom" type="datetime" value="0001-01-01 00:00:00">
7
  • What value are you setting model.ValidationValidFrom (and please confirm the data type and datatype anotation) in your model in the controller? Commented Sep 6, 2016 at 8:05
  • With EditorFor, you don't need to specify ID and it will likely break your binding. Commented Sep 6, 2016 at 8:06
  • @freedomn-m tried to remove the ID, but it didn't work either. Commented Sep 6, 2016 at 8:34
  • Sorry - meant to mark that comment as off-topic. It's not relevant to displaying the data - it will just make it harder when you post to that model - but if you're posting to a different model, it's fine. Commented Sep 6, 2016 at 8:46
  • Further to the query above: what does your EditFor render as (with value) before you call the js .datepicker? Commented Sep 6, 2016 at 8:47

1 Answer 1

4

The EditorFor generates an input and sets the value to the value of the model's property.

In this case, the model's property is model.ValidationValidFrom.

Without confirmation, this is most likely a DateTime, ie:

public class MyModel { 
    public DateTime ValidationValidFrom { get; set; } 

if a datetime is not given a value, it will be the default value: 0001-01-01.

You can set the value in the controller:

[HttpGet]
public ActionResult MyView() {
    var model = new MyModel();
    model.ValidationValidFrom = DateTime.Today;  // or .Now to include time
    return View(model);

The reason the js defaultDate is not changing it from 0001-01-01 is because the <input> already has a value, so can't be set to a 'default'.

You could change this by changing the property to a DateTime? and leaving it null in the controller. This would generate an input with value="" and then the js should give it a default value.

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

1 Comment

Thanks I'll look into this, this looks more logical. You'll hear from me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.