1

I am working with ASP.Net MVC with Jquery Date picker.

here is my model class

public class myViewModel
{
   public DateTime? FromDate {get;set;}
   public DateTime? ToDate {get;set;}
}

here is my View Class

@model testApp.myViewModel

@Html.TextBox("FromDate","{0:MM/dd/yyyy}", new { @class = "datepicker" })
@Html.TextBox("ToDate","{0:MM/dd/yyyy}", new { @class = "datepicker" })

and JQuery

<script>
    $(function () {
        $(".datepicker").datepicker({
            changeMonth: true,
            changeYear: true
        });
    });
</script>

Now the issue is whenever my page is load default date does not display in textbox instead "{0:MM/dd/yyyy}" display in both textbox

So how can I display and bind current date value in my FromDate and ToDate textbox?

Thanks

9
  • any errors in the console? do you have jquery and jquery UI included on the page?
    – DLeh
    Commented Jan 12, 2015 at 13:39
  • there is no error in console,also I have added Jquery and jqueryUI on the page
    – Sandip
    Commented Jan 12, 2015 at 13:39
  • possible duplicate: stackoverflow.com/a/22559270/526704
    – DLeh
    Commented Jan 12, 2015 at 13:40
  • no I am not binding data from model.I am using just @Html.TextBox().I don't want to use @Html.TextBoxFor()
    – Sandip
    Commented Jan 12, 2015 at 13:41
  • 1
    Your using the overload of @Html.Textbox() where the second parameter is the value (to display). You may be confusing this with @Html.TextBoxFor() where the second parameter is the format
    – user3559349
    Commented Jan 12, 2015 at 22:29

2 Answers 2

0

use this,

<script>
    $(function () {
        $(".datepicker").datepicker({
            changeMonth: true,
            changeYear: true,
            setDate : new Date()
        });
    });
</script>
0

Make sure the following using statements are included in your Model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

Change Model as follows

[Display(Name = "Date")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyy}", ApplyFormatInEditMode = true)]
    public Nullable<System.DateTime> Date { get; set; }  

Your JQuery is

<script>
$(function () {
    $(".datepicker").datepicker({ dateFormat: "MM/dd/yy", changeMonth: true, changeYear: true });
});
</script>

And View Class is

@Html.TextBoxFor(model => model.FromDate, "{0:MM/dd/yyyy}", new { id = "datepicker" })

@Html.TextBoxFor(model => model.ToFromDate, "{0:MM/dd/yyyy}", new { id = "datepicker" })

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.