How to add Date Picker Bootstrap 3 on MVC 5 project using the Razor engine?

后端 未结 9 845
执念已碎
执念已碎 2020-11-28 20:13

I need some guide lines on how to install a Date Picker Bootstrap 3 on a MVC 5 project using the Razor engine. I found this link here but couldn\'t make it work in VS2013. <

相关标签:
9条回答
  • 2020-11-28 20:29
    [DataType(DataType.Date)]
    public DateTime BirthDate { get; set; }
    

    decorate your model like this. can use a bootstrap date time picker I used this one. https://github.com/Eonasdan/bootstrap-datetimepicker/

    I suppose you already have bundles for bootstrap css and js

    Your date picker bundle entries

     bundles.Add(new ScriptBundle("~/bundles/datePicker").Include(
               "~/Scripts/moment.min.js",
               "~/Scripts/bootstrap-datetimepicker.min.js"));
    
            bundles.Add(new StyleBundle("~/Content/datepicker").Include(
                     "~/Content/bootstrap-datetimepicker.min.css"));
    

    Render bundles on your Layout View

    @Styles.Render("~/Content/datepicker")
    @Scripts.Render("~/bundles/datePicker")
    

    Your HTML in view

    <div class="form-group">
            @Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "lead col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.BirthDate, new { @class = "datetimepicker form-control" })
                @Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })
            </div>
    </div>
    

    At the end of your view add this.

    <script>
        $(document).ready(function () {
            $('.datetimepicker').datetimepicker({
                format: 'lll'
            });
        });
    </script>
    
    0 讨论(0)
  • 2020-11-28 20:34

    I hope this can help someone who was faced with my same problem.

    This answer uses the Bootstrap DatePicker Plugin, which is not native to bootstrap.

    Make sure you install Bootstrap DatePicker through NuGet

    Create a small piece of JavaScript and name it DatePickerReady.js save it in Scripts dir. This will make sure it works even in non HTML5 browsers although those are few nowadays.

    if (!Modernizr.inputtypes.date) {
        $(function () {
    
           $(".datecontrol").datepicker();
    
        });
    }
    

    Set the bundles

    bundles.Add(New ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/bootstrap-datepicker.js",
                  "~/Scripts/DatePickerReady.js",
                  "~/Scripts/respond.js"))
    
    bundles.Add(New StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/bootstrap-datepicker3.css",
                  "~/Content/site.css"))
    

    Now set the Datatype so that when EditorFor is used MVC will identify what is to be used.

    <Required>
    <DataType(DataType.Date)>
    Public Property DOB As DateTime? = Nothing
    

    Your view code should be

    @Html.EditorFor(Function(model) model.DOB, New With {.htmlAttributes = New With {.class = "form-control datecontrol", .PlaceHolder = "Enter Date of Birth"}})
    

    and voila

    enter image description here

    0 讨论(0)
  • 2020-11-28 20:34

    Attempting to provide a concise update, based on Enzero's answer.

    • Install the Bootstrap.Datepicker package.

      PM> install-package Bootstrap.Datepicker
      ...
      Successfully installed 'Bootstrap.Datepicker 1.7.1' to ...
      
    • In AppStart/BundleConfig.cs, add the related scripts and styles in the bundles.

      bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
            //...,
            "~/Scripts/bootstrap-datepicker.js",
            "~/Scripts/locales/bootstrap-datepicker.YOUR-LOCALE-CODE-HERE.min.js"));
      
      bundles.Add(new StyleBundle("~/Content/css").Include(
            ...,
            "~/Content/bootstrap-datepicker3.css"));
      
    • In the related view, in the scripts' section, enable and customize datepicker.

      @section scripts{
          <script type="text/javascript">
              //...
              $('.datepicker').datepicker({
                  format: 'dd/mm/yyyy', //choose the date format you prefer
                  language: "YOUR-LOCALE-CODE-HERE",
                  orientation: 'left bottom'
              });
          </script>
      
    • Eventually add the datepicker class in the related control. For instance, in a TextBox and for a date format in the like of "31/12/2018" this would be:

      @Html.TextBox("YOUR-STRING-FOR-THE-DATE", "{0:dd/MM/yyyy}", new { @class = "datepicker" })
      
    0 讨论(0)
提交回复
热议问题