MVC 中的 DispalyFormat 剃刀未正确显示
我有一个可编辑页面,我想重新格式化大部分字段,假设我有 2 个字段:里程和价格。 我需要在 TextBoxFor
Model.cs
public class TestModel {
[DisplayName("Mileage: ")]
[DisplayFormat(DataFormatString = "{0:##,###,###}")]
[RegularExpression(@"^(?:\d+|\d{1,3}(?:,\d{3})*)$", ErrorMessage = "*")]
public Int32 Mileage { get; set; }
[DisplayName("Price: ")]
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = false)]
public Int32 Price{ get; set; }
}
Controller.cs
public ActionResult Index() {
var model = new TestModel();
model.Mileage=150000;
model.Price=21900;
return View(model);
}
Index.cs
@using (Html.BeginForm()) {
@Html.TextBoxFor(m => m.Mileage, new { @class = "w200" })
@Html.TextBoxFor(m => m.Price, new { @class = "w200" })
<input type="submit" />
中显示它们}
不幸的是,我没有看到价格和里程的格式。
任何尽快的帮助将不胜感激,
I have an editable page, I want to reformat most of the fields, let's say that I have 2 fields, Mileage and Price.
I need to display them in TextBoxFor
Model.cs
public class TestModel {
[DisplayName("Mileage: ")]
[DisplayFormat(DataFormatString = "{0:##,###,###}")]
[RegularExpression(@"^(?:\d+|\d{1,3}(?:,\d{3})*)$", ErrorMessage = "*")]
public Int32 Mileage { get; set; }
[DisplayName("Price: ")]
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = false)]
public Int32 Price{ get; set; }
}
Controller.cs
public ActionResult Index() {
var model = new TestModel();
model.Mileage=150000;
model.Price=21900;
return View(model);
}
Index.cs
@using (Html.BeginForm()) {
@Html.TextBoxFor(m => m.Mileage, new { @class = "w200" })
@Html.TextBoxFor(m => m.Price, new { @class = "w200" })
<input type="submit" />
}
Unfortunately I don't see neither the price nor the mileage being formatted.
Any help as soon as possible will be appreciated,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
ApplyFormatInEditMode
设置为true
您可能还想根据 MVC 最佳实践切换到
EditorFor
而不是TextBoxFor
,但与你的问题无关。Set
ApplyFormatInEditMode
totrue
You may also want to switch to
EditorFor
instead ofTextBoxFor
just in terms of MVC best practices, but its not related to your problem.