Reputation: 41
I have a question regarding the jquerydatepicker and the TextBoxFor() method. I have an asp.net form in which I use them to create and edit an object. I use the same form for editing and creating. It looks like this:
<script>
$(function () {
$("#datepicker2").datepicker(
{
dateFormat: 'yy.mm.dd'
});
});
</script>
<div class=demo><%= Html.TextBoxFor(m => m.Distribution.ToDate, new { id = "datepicker2", type = "text" })%></div>
It works fine, but my question is, how do I format the date when I use edit? For create it's okay, because it will display an empty textbox and if I select a date, it will be in the correct format. In edit, it first displays the whole datetime value, and it I select a date, it displays that one the way I want. But how do I do it, so that it will display it the way I want it from the beginning?
I tried to do it like so:
<%= Html.TextBoxFor(m => m.Distribution.ToDate.ToShortDateString(), new { id = "datepicker2", type = "text" })%>
but it gives an error.
Any suggestions?
Upvotes: 1
Views: 5833
Reputation: 21
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('#startDatePicker').datepicker({ clickInput: true });
});
</script>
<% var attributes = (new Dictionary<string, object> { { "Value", Model.Plan.StartDate.ToShortStrin() } }); %>
<% attributes.Add("style", "width:80px; text-align:right"); %>
<% attributes.Add("ID", "startDatePicker"); %>
<%: Html.TextBoxFor(model => model.Plan.StartDate, attributes)%>
Upvotes: 2