0

Bootstrap DatePicker is resetting to default date after page reload. If I don't reload the page I'm able to keep selected date

<div class="form-group"> 
  <label>Action Date </label>
  <div class='input-group date' id='dpServiceDate'> 
    <input type='text' class="form-control" id="actionDate" name="actionDate" /> 
    <span class="input-group-addon"> 
      <span class="glyphicon glyphicon-calendar"></span> 
    </span>
  </div>
</div>
$(document).ready(function() {
  $('#dpServiceDate').datetimepicker({
    defaultDate: "@Model.ActionDate.Date.ToString("MM-dd-yyyy")",
    format: 'MM-DD-YYYY',
    maxDate: moment().subtract(1, 'days')
  }).on('dp.change', function(e) {
    location.href = "/XYZ/DashBoard/Index?Id=" + @Model.ClientId + "&ActionDate=" + $("#actionDate").val();
  })
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Sara
  • 11
  • 3
  • – Sara Aug 28 '19 at 19:07
  • Firstly, please use the `edit` link under your question to add more information to it. Secondly, this sounds like expected behaviour. HTML does not store state, so as soon as you change page (or reload the current one) any changes you made will be lost. You need to save them somewhere, then load them again – Rory McCrossan Aug 28 '19 at 19:20
  • @RoryMcCrossan I'm sending selected date to model and after page reload setting date from model, so ideally it should keep selected value during reload. – Sara Aug 28 '19 at 19:33
  • Set a cookie, use the cookie as the default value. – TRose Aug 28 '19 at 20:10
  • @TRose Can you please provide some example as i'm new to this – Sara Aug 28 '19 at 20:39
  • https://stackoverflow.com/a/17521905/5000465 – TRose Aug 28 '19 at 20:40

1 Answers1

0

Add useCurrent: false

$(document).ready(function() {
    $('#dpServiceDate').datetimepicker({
        defaultDate: "@Model.ActionDate.Date.ToString("MM-dd-yyyy")",
        format: 'MM-DD-YYYY',
        useCurrent: false,
        maxDate: moment().subtract(1, 'days')
    }).on('dp.change', function(e) {
        location.href = "/XYZ/DashBoard/Index?Id=" + @Model.ClientId + "&ActionDate=" + $("#actionDate").val();
    })
});
VincenzoC
  • 30,117
  • 12
  • 90
  • 112
Sara
  • 11
  • 3