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 9172113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:16:06+00:00 2026-06-17T16:16:06+00:00

I have Model properties public DateTime SelectedDate { get; set; } public DateTime SelectedDateTo

  • 0

I have Model properties

 public DateTime SelectedDate { get; set; }
 public DateTime SelectedDateTo { get; set; }

The view has

<input id="Testdate-Field" type="text"  style="width: 125px;" value="@Html.DisplayTextFor(m=>m.SelectedDate)" /> To
   <input id="TestdateTo-Field" type="text"  style="width: 125px;" value="@Html.DisplayTextFor(m=>m.SelectedDateTo)" />

To show the calendar in the view I have

  $("#Testdate-Field").bind("click", function(event) {       
     $( "#Testdate-Field").datepicker();
    });    

      $("#TestdateTo-Field").bind("click", function(event) {       
     $( "#TestdateTo-Field").datepicker();
    });    

Now, 2 problems

  1. When I click in the input box for the first time, the calendar doesn’t appear till I click somewhere else and then in the input box for date.
  2. When I pick a date from the calendars my model properties SelectedDate and SelectedDateTo do not change and remain the default values when I read them. What do I need to do to read the input dates?

—Update—
Added this in ready as per the suggestion

 $("#Testdate-Field").datepicker();
 $("#TestdateTo-Field").datepicker();

and the first problem is solved! Now how do I bind what I have selected in the datefield to my model properties?

  • 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-06-17T16:16:08+00:00Added an answer on June 17, 2026 at 4:16 pm

    For question 1, do not put your .datepicker() code in the bind event. Simply execute those on document ready:

    <script>
        jQuery(document).ready(function ($) {
            $( "#Testdate-Field").datepicker();
            $( "#TestdateTo-Field").datepicker();
        });
    </script>
    

    For your second question, use Html helpers to generate your inputs and then in your controller you can easily bind them: instead of using DisplayTextFor inside of input tags YOU generate, use TextBoxFor to generate the input tags for you:

    ViewModel:

    public class SampleViewModel
    {
        // other properties here
        public DateTime SelectedDate { get; set; }
        public DateTime SelectedDateTo { get; set; }
        // other properties here
    }
    

    View:

    @model SampleClassViewModel
    @using(Html.BeginForm())
    {
        @Html.TextBoxFor(m => m.SelectedDate, new { class = "datepickers" })
        @Html.TextBoxFor(m => m.SelectedDateTo, new { class = "datepickers" })
        <input type="submit" />
    }
    <script>
        jQuery(document).ready(function ($) {
            $( ".datepickers").datepicker();
        });
    </script>
    

    There are many way to bind your properties, here are a couple…
    Controller option 1:

    [HttpPost]
    public ActionResult SomeControllerAction(SampleViewModel model)
    {
        // do work...
    }
    

    Controller option 2:

    [HttpPost]
    public ActionResult SomeControllerAction()
    {
        var model = SomeServiceMethodToBuildModel();
        this.TryUpdateModel(model);
        // do work...
    }
    

    Thanks!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this model: public class User { //other properties public List<Task> Tasks {get;set;}
I have properties declared in my view model like: [Required(ErrorMessage = The Date field
I have a model public class Foo{ public int Id{get;set;} public string Name {get;
I have this entity service in my domain model with two datetime type properties
I relised I have lots of models view models with those two properties public
I have a view model with a couple of properties that are not displaying
I have model Foo which has field bar. The bar field should be unique,
I have model Article it has field title with some text that may contain
If I have in my model class a property of type DateTime how can
I have a Domain model that countains the following properties : public class Profile

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.