【发布时间】:2016-09-30 06:11:09
【问题描述】:
在我的 MVC 5 视图中设置格式时遇到问题。视图是作为 TextAreaFor 的脚手架。我在 ViewModel 中为我的属性添加了 DataFormatting,但它实际上并没有为我设置格式。根据我研究的所有内容,我正确地格式化了 ViewModel,但我无法弄清楚为什么格式化实际上不起作用。
所以,我尝试使用@Html.EditFor。这似乎使日期格式化工作,但它没有应用引导程序“表单控制”类。然后我尝试了@Html.TextBoxFor,它的响应类似于@Html.TextAreaFor,没有格式化,但确实应用了 Bootstrap 类。
我不知道要使用哪一个,我还需要做什么才能让我的 View 不仅格式化日期而且还应用 Bootstrap CSS。
这是我的 ViewModel 属性。
[DisplayName("Call Date")]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime? CallDate { get; set; }
这是脚手架创建的原始 TextAreaFor。没有格式化,但 Bootstap CSS 可以工作。
<div class="form-group">
@Html.LabelFor(model => model.CallDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.CallDate, new { @class = "form-control datepicker" })
@Html.ValidationMessageFor(model => model.CallDate, "", new { @class = "text-danger" })
</div>
</div>
这是格式化日期但不应用 Bootstrap CSS 的 EditFor。
<div class="form-group">
@Html.LabelFor(model => model.CallDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CallDate, new { @class = "form-control datepicker" })
@Html.ValidationMessageFor(model => model.CallDate, "", new { @class = "text-danger" })
</div>
</div>
这是像 TextAreaFor 一样响应的 TextBoxFor。
<div class="form-group">
@Html.LabelFor(model => model.CallDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.CallDate, new { @class = "form-control datepicker" })
@Html.ValidationMessageFor(model => model.CallDate, "", new { @class = "text-danger" })
</div>
</div>
【问题讨论】:
标签: asp.net-mvc twitter-bootstrap asp.net-mvc-4 asp.net-mvc-5