Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Html.TextBoxFor中格式化DateTimeOffset?_C#_Twitter Bootstrap 3_Data Annotations_Asp.net Mvc 5_Neo4jclient - Fatal编程技术网

C# 如何在Html.TextBoxFor中格式化DateTimeOffset?

C# 如何在Html.TextBoxFor中格式化DateTimeOffset?,c#,twitter-bootstrap-3,data-annotations,asp.net-mvc-5,neo4jclient,C#,Twitter Bootstrap 3,Data Annotations,Asp.net Mvc 5,Neo4jclient,我有一个基于Bootstrap3和ASP.NETMVC5构建的视图,用于编辑用户配置文件信息,但它在表单上显示的格式不正确 数据库是Neo4j,我使用Neo4jClient与.NET中的数据库进行交互。Neo4jClient需要使用DateTimeOffset对象而不是DateTime对象来使用Json.NET的序列化程序来传递到Neo4j的REST接口。因此,我的模型使用DateTimeOffset对象来存储用户的生日 以下是我视图中的“我的表单”字段: <div class="form

我有一个基于Bootstrap3和ASP.NETMVC5构建的视图,用于编辑用户配置文件信息,但它在表单上显示的格式不正确

数据库是Neo4j,我使用Neo4jClient与.NET中的数据库进行交互。Neo4jClient需要使用
DateTimeOffset
对象而不是
DateTime
对象来使用Json.NET的序列化程序来传递到Neo4j的REST接口。因此,我的模型使用DateTimeOffset对象来存储用户的生日

以下是我视图中的“我的表单”字段:

<div class="form-group">
    <label class="col-md-4 control-label" for="Birthday">Birthday</label>
    <div class="col-md-4">
        @Html.TextBoxFor(model => model.Birthday, new { @class = "form-control input-md datepicker", placeholder = "Birthday" })
        <span class="help-block">Please enter your birthday</span>
    </div>
</div>
但是,它应该有这种格式,因为我们只需要日期部分:

MM/dd/yyyy
我已尝试在model属性上使用DataFormatString注释,但它仍然没有以正确的格式显示:

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTimeOffset Birthday { get; set; }
此注释是否仍适用于DateTimeOffset对象?如何修复字符串格式?

回答如下:

摘要:
TextBoxFor
不尊重
DataFormatString
,您需要使用
EditorFor
,但它不支持自定义HTML属性,因此您需要一个编辑器模板(
Views\Shared\EditorTemplates\DateTimeOffset.cshtml

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTimeOffset Birthday { get; set; }