货币格式的数据注释不起作用 [英] Data Annotation for currency format not working

查看:69
本文介绍了货币格式的数据注释不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my ASP.NET MVC Core web project on VS2015, the following model is displaying data as, e.g., 15481 instead of $15,481 even though I'm using [DisplayFormat] below:

模型:

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; }
}

ModelView :

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>

推荐答案

仅当使用@Html.DisplayFor()@Html.EditorFor()时,才会使用[DisplayFormat]属性.使用TextBoxFor()时将被忽略.

A [DisplayFormat] attribute is only respected when using @Html.DisplayFor() or @Html.EditorFor(). It is ignored when using TextBoxFor().

此外,如果要与@Html.EditorFor(r => r[i].SaleAmount)一起使用,则需要修改属性以包括

In addition, if you wanted to use it with @Html.EditorFor(r => r[i].SaleAmount) you need to modify the attribute to include the ApplyFormatInEditMode property

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

但这对您没有多大用处,因为尽管它将正确显示在文本框中,但它不会绑定回到您的float属性,除非您还创建了一个转换(例如)转换的自定义模型装订器"$15,481"返回到float

however that would be of little use to you, because although it would display in the textbox correctly, it will not bind back to you float property unless you were also to create a custom model binder which converted (say) "$15,481" back to a float

这篇关于货币格式的数据注释不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆