Search code examples
asp.net-mvctwitter-bootstrapbootstrap-datetimepicker

Bootstrap Datetimepicker not working MVC 4


I have been tryng to use this datetimepicker for the last few days and no matter what i try the best i can get is the calendar appearing in the top left hand corner. I cannot find any real tutorials on how to implement the control and past answers on this site havent helped either.

Here is what i have now:

View

@Scripts.Render("~/Scripts/knockout-3.4.0.js")
@Scripts.Render("~/Scripts/jquery-1.10.2.js")
@Scripts.Render("~/bundles/bootstrap")
@Styles.Render("~/Content/css")
<script type="text/javascript" src="/Scripts/jquery.min.js"></script>
<script type="text/javascript" src="/Scripts/moment.min.js"></script>
<script type="text/javascript" src="/Scripts/bootstrap.min.js"></script>
<script type="text/javascript" src="/Scripts/bootstrap-datetimepicker.js"></script>
<link rel="stylesheet" href="/Content/bootstrap-datetimepicker.css" />

<div class="container">
  <div class="row">
    <div class='col-sm-6'>
      <div class="form-group">
        <div class='input-group date' id='datetimepicker1'>
          <input type='text' class="form-control" />
          <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
          </span>
        </div>
      </div>
    </div>

  </div>
</div>

<script>
  $(function() {
    $('#datetimepicker1').datetimepicker();
  });
</script>

image of view


Solution

  • In reference to addressing your second issue with the calendar glyphicon being spaced apart from the input you need to do either of these (your preference):

    <div class="row">
      <div class='col-sm-6' /*change this to col-sm-3 or any that is below 6*/>
      <div class="form-group">
        <div class='input-group date' id='datetimepicker1'>
          <input type='text' class="form-control" />
          <span class="input-group-addon">
              <span class="glyphicon glyphicon-calendar></span>
          </span>
        </div>
      </div>
    </div>
    

    OR:

    <div class="row">
      <div style="width: 25%;/*or something close to that*/" class='col-sm-6'>
        <div class="form-group">
          <div class='input-group date' id='datetimepicker1'>
            <input type='text' class="form-control" />
            <span class="input-group-addon">
                 <span class="glyphicon glyphicon-calendar"></span>
            </span>
          </div>
        </div>
      </div>
    </div>
    

    Also just a nit picky thing.. try to keep your Scripts and Styles together and not mixed between each other.. makes for easier reading.