我有一个可编辑的页面,我想重新格式化大部分字段,让我们说我有2个字段,Mileage和Price。 我需要在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" />
}
不幸的是,我没有看到价格和里程都没有格式化。
欢迎任何帮助,
答案 0 :(得分:3)
将ApplyFormatInEditMode
设为true
[DisplayFormat(DataFormatString = "{0:##,###,###}", ApplyFormatInEditMode=true)]
您可能还希望仅根据MVC最佳做法切换到EditorFor
而不是TextBoxFor
,但它与您的问题无关。