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

后端 未结 9 844
执念已碎
执念已碎 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:13

    This answer simply applies the type=date attribute to the HTML input element and relies on the browser to supply a date picker. Note that even in 2017, not all browsers provide their own date picker, so you may want to provide a fall back.

    All you have to do is add attributes to the property in the view model. Example for variable Ldate:

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    Public DateTime Ldate {get;set;}
    

    Assuming you're using MVC 5 and Visual Studio 2013.

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

    Simple:

    [DataType(DataType.Date)]
    Public DateTime Ldate {get;set;}
    
    0 讨论(0)
  • 2020-11-28 20:17

    Checkout Shield UI's Date Picker for MVC. A powerful component that you can integrate with a few lines like:

    @(Html.ShieldDatePicker()
        .Name("datepicker"))
    
    0 讨论(0)
  • 2020-11-28 20:18

    1.make sure you ref jquery.js at first
    2.check layout,make sure you call "~/bundles/bootstrap"
    3.check layout,see render section Scripts position,it must be after "~/bundles/bootstrap"
    4.add class "datepicker" to textbox
    5.put $('.datepicker').datepicker(); in $(function(){...});

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

    This answer uses the jQuery UI Datepicker, which is a separate include. There are other ways to do this without including jQuery UI.

    First, you simply need to add the datepicker class to the textbox, in addition to form-control:

    <div class="form-group input-group-sm">
        @Html.LabelFor(model => model.DropOffDate)
        @Html.TextBoxFor(model => model.DropOffDate, new { @class = "form-control datepicker", placeholder = "Enter Drop-off date here..." })
        @Html.ValidationMessageFor(model => model.DropOffDate)
    </div>
    

    Then, to be sure the javascript is triggered after the textbox is rendered, you have to put the datepicker call in the jQuery ready function:

    <script type="text/javascript">
        $(function () { // will trigger when the document is ready
           $('.datepicker').datepicker(); //Initialise any date pickers
        });
    </script>
    
    0 讨论(0)
  • 2020-11-28 20:23

    Add just these two lines before the datetime property in your model

    [DataType(DataType.Date)] 
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    

    and result will be :Date Picker in MVC Application

    0 讨论(0)
提交回复
热议问题