1

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?

1 Answer 1

2
<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)%>
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.