Hari Gillala
Hari Gillala

Reputation: 11916

Date Picker in MVC ASP.NET

I want to use Data Picker for a datatime field in my MVC-Form

I am using the Following code to get the Data time value,

[Required(ErrorMessage="Please enter the Absence Start Date")]
    [StringLength(20,ErrorMessage="The Format must not exceed 50 Characaters")]
    [DataType(DataType.DateTime )]
    [Display(Name="Absence Start Date")]
    public DateTime AbsenceStartDate{get;set;}

But I want to have datapicker controls beside the textbox. How could I do that?

Any help

Thank you

Upvotes: 0

Views: 1585

Answers (4)

Ashraf Alam
Ashraf Alam

Reputation: 3650

You can create an extension method as below:

 public static MvcHtmlString CalenderTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
    {
        return htmlHelper.TextBoxFor(expression, "{0:M/dd/yyyy}", new { @class = "datepicker text" });
    }

Then use in view as below:

@Html.CalenderTextBoxFor(model => model.Employee.HireDate)

Upvotes: 0

Mayo
Mayo

Reputation: 10782

I would use a client-side date picker. There are plenty available for jQuery. Then you use ASP.NET MVC to validate the input as text in the form of a date..

Upvotes: 0

archil
archil

Reputation: 39501

You may use jQuery UI datepicker with DateTime editor template in asp.net mvc. Take a look at Sample. Actually, you SHOULD use editor-template

Upvotes: 1

StuperUser
StuperUser

Reputation: 10850

A good client-side datepicker is provided in the jQuery UI library: http://jqueryui.com/demos/datepicker/

Upvotes: 1

Related Questions