帮助用户在 EditorFor 中输入正确的格式
Posted
技术标签:
【中文标题】帮助用户在 EditorFor 中输入正确的格式【英文标题】:Helping user input correct format into EditFor 【发布时间】:2019-12-08 22:34:49 【问题描述】:我的 SQL 数据库有一列 Date
,它只允许使用以下格式的字符串:__/____
,基本上是月/年,例如:01/2019
。
我的可以吗:
@html.EditorFor(model => model.Date, new htmlAttributes = new @class = "form-control" )
有一些帮助,以便用户只能使用正确的格式输入?例如强类型的编辑器是这样的:__/___
,并且只能输入数字来填充下划线。
【问题讨论】:
如果您将数据库和模型更改为存储日期而不是字符串(无论如何我都会建议),我认为您可以使用 this answer 中的技术。 用户界面中字段的数据格式和数据存储可以是独立的。您应该在用户区域设置中显示日期并在保存到数据库之前转换为格式 这会有所帮助吗:<input type="month">
? (developer.mozilla.org/en-US/docs/Web/HTML/Element/input/month)
@Alex 没有那么多,因为 SQL 以这种格式将值存储为 nvarchar。
@GarethD 如果我将数据类型更改为DateTime
,我可以使用@Alex 方法输入month
吗?
【参考方案1】:
@Html.TextBoxFor(m => m.StartDate,
new @Value = Model.StartDate.ToString("yyyy/MM/dd"), @class="datepicker" )
【讨论】:
以上是关于帮助用户在 EditorFor 中输入正确的格式的主要内容,如果未能解决你的问题,请参考以下文章