0

I am using a jquery datepicker in my ASP.NET MVC4 Web app. I am also using an editor template for my datetime data type.

My editor template looks like this:

<input type="text" name="@ViewData.ModelMetadata.PropertyName" class="date" value='@(((DateTime)Model).ToShortDateString())' />

My code for jquery date picker:

$(document).ready(function() {
    $(".date").datepicker();
})

The problem I am running into is that when I load date from the database, the date picker highlights today's date instead of the date in the textbox. How do I get the date picker to highlight the date I loaded into the textbox?

1
  • This doesn't help me since I don't know what the date will be ahead of time. Commented Apr 16, 2013 at 22:06

1 Answer 1

1
$(".date").datepicker({defaultDate: "@(((DateTime)Model).ToShortDateString()"});

Surely though, if you have set @model DateTime at the top of your editor template, you could simply go:

$(".date").datepicker({defaultDate: "@Model.ToShortDateString()"});

Or in fact, maybe even dynamically like so - N.B. the id attribute:

<input type="text" id="@ViewData.ModelMetadata.PropertyName" name="@ViewData.ModelMetadata.PropertyName" class="date" value='@(((DateTime)Model).ToShortDateString())' />

$(".date").datepicker({defaultDate: $("#@ViewData.ModelMetadata.PropertyName").val()});

Have a play around.

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

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.