jQuery Datetime picker MVC3

后端 未结 4 1960
长发绾君心
长发绾君心 2021-01-12 12:36

I have in my Model this field

[DataType(DataType.DateTime)]
    [Required(ErrorMessage = \"Expire is required\")]
    public DateTime Expire
    {
      get;         


        
相关标签:
4条回答
  • 2021-01-12 12:45
    @inherits System.Web.Mvc.WebViewPage<System.DateTime> 
    

    should be

    @inherits System.Web.Mvc.WebViewPage<System.DateTime?>
    
    0 讨论(0)
  • 2021-01-12 12:47

    This is the code of my editor template: The point is to avoid null values:

    @model DateTime?
    
    
    <script src="../../../Scripts/jquery-1.4.4.js" type="text/javascript"></script>
    <script src="../../../Scripts/jquery-ui.js" type="text/javascript"></script>
    
               @Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : String.Empty), new { @class = "datePicker" })
    <script type='text/javascript'>
        $(document).ready(function () {
            $(".datePicker").datepicker({
                //      buttonImage: "/content/images/calendar.gif",
                //      showOn: "both",
                //      defaultDate: $("#calendar-inline").attr('rel')
                showAnim: 'slideDown',
                dateFormat: 'dd/mm/yyyy'
    
            });
        });
    </script>
    
    0 讨论(0)
  • 2021-01-12 12:57

    Just put ? in your System.DateTime

    example :

    @model System.DateTime?
    
    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
                    new
                    {
                        data_datepicker = true
                    })
    

    if you ommit the question mark you will ge an error

    0 讨论(0)
  • 2021-01-12 13:01

    Try one of the following:

    • If the action is for creating a new object pass a new instance as model, e.g. return View(new MyObject())
    • Change @inherits System.Web.Mvc.WebViewPage<System.DateTime> to @inherits System.Web.Mvc.WebViewPage<System.DateTime?>
    0 讨论(0)
提交回复
热议问题