ASP.Net MVC DatePicker

Pieter van der Westhuizen, a professional freelance web & mobile developer and founder of Coalition Software.
Search for a command to run...

Pieter van der Westhuizen, a professional freelance web & mobile developer and founder of Coalition Software.
A recent requirement came up for configurable dynamic forms in a mobile application. There are a few dynamic form packages for Flutter, but I wanted a bit more control over the schema and behavior of the form. Here follows my attempt at creating dyna...

If you've ever written a mobile app with Flutter that integrates with an API, then you'll know that there are a lot of model objects that get passed between the app and the API. JSON Serialization in Flutter can also take a fair amount of boilerplate...

Back in May 2020, Microsoft announced a new feature for Azure Blob Storage called Blob Index. Essentially, this enables you to add key/value tags to your Blob objects and be able to query said Blob objects without having to use a separate service lik...

In my last post, "Using AWS Cognito with Xamarin Forms", I showed how to authenticate with Amazon Cognito using Xamarin Forms and the Xamarin.Essentials Web Authenticator. In this post, we'll go through the process of using the AWS Cognito Hosted UI ...

In this post, I'll show you how to quickly and easily set up user authentication for your Xamarin Forms app using Amazon Cognito. AWS Cognito is a user identity management solution by Amazon. It is a really easy way to add authentication to your appl...

I recently worked on a project that had a big form with a lot of date fields. I generally believe it is good practise to show the user a calendar when they either click in the date field or on a calendar icon next to the field.
jQuery UI’s Datepicker does a very good job at making this easy. All I had to do is add a class to the text boxes i would like to use as datepickers:
@Html.TextBoxFor(model => model.DoB, new { @class = "datepicker" })
And add a piece of Javascript to add a datepicker to all the elements which has a class datepicker:
$('.datepicker').datepicker({
showOn: "both",
buttonImage: "../public/images/Calendar_16x16.png",
buttonImageOnly: "true"
});
Also, you need to make sure that you have referenced the jQuery and jQuery UI libraries in order for this to work.
If all goes well you should have an attractive icon next to your date field. Setting the showOn parameter to both, will show the date picker when the textbox has focus or when the user clicks on the icon next to the textbox.

With ASP.Net MVC this could even be easier. By creating a helper, we do not have to manually specify the class name every time we need a date picker text box. The code for the helper would look like this:
@helper DatePicker(string name, object value = null)
{
var val = value "";
<input type="text" name="@name" class="datepicker" id="@name" value="@val" />
}
Using it in your view would then look like this:
@Helpers.DatePicker("DoB",Model.DoB)
Just make sure, you have the Javascript in either your _Layout or the page in order for the jQueryUI date picker to work.