简体   繁体   English

在MVC中以字符串格式绑定可为null的日期时间

[英]Binding nullable datetime in MVC with String format

i am unable to solve problem from last two days. 最近两天我无法解决问题。

@Html.TextBoxFor(model => model.myObject.dateAssign, String.Format("{0:MM/dd/yy}"))

here, dateAssign is nullable datetime. 在这里,dateAssign是可为空的日期时间。 it may be or maybe not have value. 它可能有价值,也可能没有价值。

i am keep getting following error 我一直在跟随错误

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

please give me some hint or direction 请给我一些提示或方向

String.Format() needs a second argument (see documentation) - in your case your date. String.Format()需要第二个参数(请参阅文档) -您的情况是您的日期。

Have a look at this page for date formats. 请在此页面上查看日期格式。

Then you would write something like 然后你会写像

@Html.TextBox(model => String.Format("{0:MM/dd/yy}", model.myObject.dateAssign))  

Or, if you want to use @Html.TextBoxFor , you can annotate your property like this: 或者,如果要使用@Html.TextBoxFor ,则可以这样注释属性:

// in your model:
[DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode= true]
DateTime? dateAssign { get; set; }  

// and in your view:
@Html.TextBoxFor(model => model.myObject.dateAssign)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM