gpt4 book ai didi

c# - 如何删除 mvc4 文本框中的默认日期?

转载 作者:行者123 更新时间:2023-11-30 14:25:06 25 4
gpt4 key购买 nike

我在 mvc4 应用程序中有一个文本框,如下所示。

@Html.TextBoxFor(x=>x.dateofAction, ViewBag.filterdateTime as string,  new { @id = "dateofAction", @placeholder = "Date Of Action", @class = "txtBox form-control calender validate[required]" })

我正在为这个文本框使用 jquery 日历。

 $(document).on('focus', '.calender', function(){
$(this).datepicker({

dateFormat: "dd/mm/yy",
changeMonth: true,
changeYear: true,
showOn: 'button',
buttonImage: '/images/icondate.png',
buttonImageOnly: true
})
});

这是我的模型

 [DisplayName("dateofAction")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime dateofAction { get; set; }

当第一次页面加载默认日期 1/1/0001 12:00:00 AM 显示在文本框中时,我遇到了问题。其实我想显示占位符值。

我可以知道为什么我无法获取占位符值吗?

最佳答案

为了显示带有占位符的空文本框,您需要使您的属性可为空。如果还想选择一个日期,那么您还可以使用 [Required] 属性装饰该属性。

另请注意,您的 [DisplayFormat] 是不必要的,并且会被 TextBoxFor() 方法忽略。您还应该使用 [Display] 属性,而不是 [DisplayName] 您的属性应该是

[Display(Name= "Date of Action")]
[Required(ErrorMessage = "Please select a date")]
public DateTime? dateofAction { get; set; }

然后在 View 中你的代码应该是

@Html.TextBoxFor(x=>x.dateofAction, new { @placeholder = "Date Of Action", @class = "txtBox form-control calender validate[required]" })

并且您的日期选择器插件应该在渲染 DOM 时初始化,而不是每次聚焦时初始化

$('#dateofAction').datepicker({
dateFormat: "dd/mm/yy",
....
})

关于c# - 如何删除 mvc4 文本框中的默认日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39612860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com