在我的Person
模型中,我获得了生日财产:
[DisplayName("Date of Birth")]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime BirthDate { get; set; }
在编辑视图中,我显示了出生日期输入字段:
<div class="form-group">
@Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BirthDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })
</div>
</div>
在localhost上生成HTML:
<input class="form-control text-box single-line valid" data-val="true" data-val-date="The field Date of Birth must be a date." data-val-required="Pole Date of Birth jest wymagane." id="BirthDate" name="BirthDate" type="date" value="2014-10-02">
并在加载页面后在浏览器中显示该字段的值:
我发布了我的应用程序,但生成的HTML略有不同。它不包含单词valid
,value
格式不同:"2/5/2015"
代替"2014-10-02"
。
<input class="form-control text-box single-line" data-val="true" data-val-date="The field Date of Birth must be a date." data-val-required="The Date of Birth field is required." id="BirthDate" name="BirthDate" type="date" value="2/5/2015">
这导致在我通过datepicker设置日期之前,日期不会显示在输入字段中。
我希望它的行为与本地部署的应用程序相同。
我的总部设在波兰,该应用程序已部署到myasp.net服务器,我打赌这些服务器位于Anglo-Saxon国家,具有不同的日期格式。这可能是个问题吗?
答案 0 :(得分:1)
我认为您应该手动设置您的webProject文化。这样就不会应用部署服务器上的文化设置。
<强>的web.config 强>
<globalization uiCulture="pl" culture="pl-PL" />
请参阅MSDN - How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization
如果您担心项目的全球化,您也可以改变单个页面的文化
<强> yourPage.aspx 强>
<%@ Page UICulture="pl" Culture="pl-PL" %>
如果您不知道这一点,您可以设置自定义dataFormatString ,而不是指向适用于cultureSetting的标准格式字符串。
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime BirthDate { get; set; }