代码之家  ›  专栏  ›  技术社区  ›  Junior

如何在Asp.NetMVC公司?

  •  0
  • Junior  · 技术社区  · 7 年前

    DateTime 使用以下格式 MM/yyyy . 我的视图模型看起来像这样

    public class SomeViewModel
    {
        public string Name { get; set; }
    
        [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? TargetMonth { get; set; }
    }
    

    Date.cshtml

    @model DateTime?
    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "form-control", placeholder = ViewData.ModelMetadata.Watermark })
    

    当我像这样选择/键入值时 07/2018 在客户端验证期间,我得到以下错误

    “目标月份”字段必须是日期。

    年月日 格式?

    2 回复  |  直到 7 年前
        1
  •  2
  •   sanfalero    7 年前

    source ).

    我建议使用现有的日期选择器库,有很多好的解决方案可供选择。我发现了一个 JS Fiddle using Bootstrap Datepicker 这就满足了你的选月要求。

    Html格式:

    <input type="text" class="monthPicker" placeholder="CheckIn" >

    JS公司:

    $('.monthPicker').datepicker({
        autoclose: true,
        minViewMode: 1,
        format: 'mm/yyyy'
    })
    

        2
  •  0
  •   Byron Jones    7 年前

    您希望视图模型上的“TargetMonth”属性是字符串,而不是日期。验证字符串以匹配此正则表达式: '[0-1]?[1-9]/[1-2][0-9]{3}'