-5

I have generated Controller and View using add scaffolding in Visual Studio 2017, but the datetime input field need to enter the date time manually instead of datetime picker. I have tried several ways but still not able to implement datetime picker.

    [DataType(DataType.DateTime)]
    public DateTime EnrollmentDate { get; set; }        

The datetime can be selected when I added

    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

but I need hour and minutes as well but this is not working.

    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd-hh-mm-tt}", ApplyFormatInEditMode = true)]
2

2 Answers 2

2

DateTime picker can be implemented using jQuery's date time picker. Or if you want an inbuilt MVC datetime picker, modify your code as :

Field:

   [DataType(DataType.Date)]
   public DateTime EnrollmentDate { get; set; }  

and then in view

  <div class="form-group">
            @Html.LabelFor(model => model.EnrollmentDate , htmlAttributes: new {@class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EnrollmentDate , new {htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EnrollmentDate , "", new { @class = "text-danger" })
            </div>
        </div>

which will then render the date picker for you.

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

1 Comment

I was trying to select date and time but I think jQuery does not have datetime picker so I just use date picker
0

I believe what you're looking for is this

[DisplayFormat(DataFormatString = "{0:yyyy-MM-ddThh:mm}", ApplyFormatInEditMode = true)]

Note the capital T

Produces 20/01/2020 12:04 PM

Comments

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.