货币格式的数据注释不起作用

Data Annotation for currency format not working

在我的 ASP.NET MVC Core Web 项目 VS2015 中,以下模型将数据显示为例如 15481 而不是 $15,481,即使我使用的是 [DisplayFormat] 下面:

型号:

public class State
{
    [Key]
    public int StateId { get; set; }

    [Column(TypeName ="varchar(40)")]
    public string StateName { get; set; }

    [Column(TypeName = "char(2)")]
    public string StateCode { get; set; }
}

public class Sales
{
    [Key]
    public int SalesId { get; set; }
    public int? FiscalYear { get; set; }

    [DisplayFormat(DataFormatString = "{(0:C0)}")]
    public float? SaleAmount { get; set; }

    public int StateId { get; set; }
    public State State { get; set; }
}

模型视图:

public class StatesSalesViewModel
{
    [HiddenInput]
    public int StateId { get; set; }

    [Display(Name ="State")]
    public string StateName { get; set; }
    public int? FiscalYear { get; set; }

    [DisplayFormat(DataFormatString = "{(0:C0)}")]
    public float? SaleAmount { get; set; }
}

控制器:

public async Task<IActionResult> FYSales(List<StatesSalesViewModel> model, string GO, int currentlySelectedIndex, string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;
    ViewBag.YearsList = Enumerable.Range(1996, 29).Select(g => new SelectListItem { Value = g.ToString(), Text = g.ToString() }).ToList();

    if (!string.IsNullOrEmpty(GO))
    {
        var qryVM = from s in _context.States
                    join g in _context.Sales on s.StateId equals g.StateId
                    where g.FiscalYear == currentlySelectedIndex
                    select new StatesSalesViewModel() {StateId = s.StateId, StateName = s.StateName, SaleAmount = g.SaleAmount , FiscalYear = currentlySelectedIndex };

        return View(qryVM.ToList());
    }
}

查看:

@model IList<mProject.Models.StatesSalesViewModel>

<div class="row">
    <div class="col-md-12">
        <form asp-controller="StatesSales" asp-action="getSales" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post">
            @{
                IEnumerable<SelectListItem> yearsList = (IEnumerable<SelectListItem>)ViewBag.YearsList;
                var currentlySelectedIndex = 0; // Currently selected index (usually will come from model)
            }
            <strong>Select a Post Year</strong>
            <h6>Choose a year o begin:</h6>
            <label>Year:</label><select asp-for="@currentlySelectedIndex" asp-items="yearsList"></select><input type="submit" class="btn btn-default" name="GO" value="GO" />
            <table class="table">
                <thead>
                    <tr>
                        <th></th>
                        <th></th>
                        <th>Fiscal Year</th>
                        <th>State</th>
                        <th>Sales</th>
                    </tr>
                </thead>
                <tbody>
                    @for (int i=0; i< Model.Count(); i++)
                    {
                        <tr>
                            <td>@Html.HiddenFor(r => r[i].StateID)</td>
                            <td>@Html.HiddenFor(r => r[i].FYSalesID)</td>
                            <td>
                                @Html.TextBoxFor(r => r[i].FiscalYear)
                            </td>
                            <td>
                                @Html.TextBoxFor(r => r[i].StateName)
                            </td>
                            <td>
                                @Html.TextBoxFor(r => r[i].SaleAmount)
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
            <button type="submit" class="btn btn-default">Save</button>
        </form>
    </div>
</div>

可以使用货币注释。然而,它只是告诉 MVC 使用哪个显示或编辑器模板。正如我们所说,当前模板使用系统货币。您必须提供自定义编辑器模板或显示模板以及其他一些方法来确定要显示的货币符号。Look here 如何提供您自己的实现

试试这个

 [DisplayFormat(DataFormatString = "{0:C0}")]

示例

public class Sales
{
    [Key]
    public int SalesId { get; set; }

    [DisplayFormat(DataFormatString = "{0:C0}")]
    public float? SaleAmount { get; set; }
}

Check here 了解更多详情

只有在使用 @Html.DisplayFor()@Html.EditorFor() 时才会考虑 [DisplayFormat] 属性。使用 TextBoxFor().

时会被忽略

此外,如果您想将它与 @Html.EditorFor(r => r[i].SaleAmount) 一起使用,您需要修改属性以包含 ApplyFormatInEditMode 属性

[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
public float? SaleAmount { get; set; }

但是这对你没什么用,因为虽然它会正确显示在文本框中,但它不会绑定回你 float 属性 除非你也创建自定义将(例如)",481" 转换回 float

的模型活页夹