Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 933915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:54:38+00:00 2026-05-15T20:54:38+00:00

I’m trying to create a strongly type html helper extension for a date picker

  • 0

I’m trying to create a strongly type html helper extension for a date picker using jquery ui datepicker. I have created an extension that isn’t strongly typed, which works but now I’m trying to create it strongly typed.

Here is what I have:

public static MvcHtmlString DatePicker(this HtmlHelper html, string name, object date)
    {
        var inputTag = new TagBuilder("input");
        inputTag.MergeAttribute("id", name);
        inputTag.MergeAttribute("name", name);
        inputTag.MergeAttribute("type", "text");

        if (date != null)
        {
            string dateValue = String.Empty;
            if ((date is DateTime? ||date is DateTime) && (DateTime)date != DateTime.MinValue)
            {
                dateValue = ((DateTime)date).ToShortDateString();
            }
            else if (date is string)
            {
                dateValue = (string)date;
            }

            inputTag.MergeAttribute("value", dateValue);
        }

        return MvcHtmlString.Create(inputTag.ToString());
    }

This works and will persist the value. Here is what I have for my strongly typed extension.

public static MvcHtmlString DatePickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
    {
        Func<TModel, TProperty> myFunc = expression.Compile();
        var value = myFunc(???);

        var inputName = ExpressionHelper.GetExpressionText(expression);
        return htmlHelper.DatePicker(inputName, value);
    }

This works if I pass null for the value (value = null) but when editing, the date picker isn’t populated with a value.

How can I get the value in my DatePickerFor extension method off the model. (I know I can just pass in the value by another parameter, but I would prefer not to. Plus this is pretty interesting)

Thanks
Kevin

–EDIT——What I have as a final result———-
Note: This hasn’t been tested fully, but seems to be working right now.

public static MvcHtmlString DatePicker(this HtmlHelper html, string name, object date)
    {
        var inputTag = new TagBuilder("input");
        inputTag.MergeAttribute("id", name);
        inputTag.MergeAttribute("name", name);
        inputTag.MergeAttribute("type", "text");
        inputTag.AddCssClass("jqueryDatePicker");

        if (date != null)
        {
            string dateValue = String.Empty;
            if ((date is DateTime? ||date is DateTime) && (DateTime)date != DateTime.MinValue)
            {
                dateValue = ((DateTime)date).ToShortDateString();
            }
            else if (date is string)
            {
                dateValue = (string)date;
            }

            inputTag.MergeAttribute("value", dateValue);
        }

        return MvcHtmlString.Create(inputTag.ToString());
    }

    public static MvcHtmlString DatePickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
    {
        Func<TModel, TProperty> methodCall = expression.Compile();
        TProperty value = methodCall(htmlHelper.ViewData.Model);
        var inputName = ExpressionHelper.GetExpressionText(expression);

        return htmlHelper.DatePicker(inputName, value);
    }

This is using the JQuery UI datepicker script. What is “jqueryDatePicker” and why is it added as a class? Instead of calling $(#…).datepicker() to each page, I add this class and there is some javascript that will find this class and call .datepicker() for you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:54:39+00:00Added an answer on May 15, 2026 at 8:54 pm

    Something like the following should work:

    var input = myFunc(htmlHelper.ViewData.Model)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.