Asp.net mvc MVC中的DispalyFormat,razor未正确显示

Asp.net mvc MVC中的DispalyFormat,razor未正确显示,asp.net-mvc,asp.net-mvc-3,html-helper,Asp.net Mvc,Asp.net Mvc 3,Html Helper,我有一个可编辑的页面,我想重新格式化大多数字段,假设我有两个字段,里程和价格。 我需要在文本框中显示它们 Model.cs public class TestModel { [DisplayName("Mileage: ")] [DisplayFormat(DataFormatString = "{0:##,###,###}")] [RegularExpression(@"^(?:\d+|\d{1,3}(?:,\d{3})*)$", ErrorMessage = "*")

我有一个可编辑的页面,我想重新格式化大多数字段,假设我有两个字段,里程和价格。 我需要在文本框中显示它们

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; }
}
控制器

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" />
@使用(Html.BeginForm()){
@Html.TextBoxFor(m=>m.里程,新的{@class=“w200”})
@TextBoxFor(m=>m.Price,新的{@class=“w200”})
}

不幸的是,我看不到价格和里程的格式


如果您能尽快提供帮助,我们将不胜感激。

ApplyFormatInEditMode
设置为
true

[DisplayFormat(DataFormatString = "{0:##,###,###}", ApplyFormatInEditMode=true)]

您可能还想切换到
EditorFor
,而不是
TextBoxFor
,这仅仅是因为MVC最佳实践,但与您的问题无关。

我尝试过,但它给出了以下例外情况:“传入字典的模型项的类型为'System.Int32',但此字典需要'System.String'类型的模型项。@AlaaOsta您是指EditorFor stuff,还是ApplyFormatInEditMode?对于EditorFor stuff,如果我将其更改为TextBoxFor,页面将加载良好,但值根本没有格式化。”。