Data Annotation for currency format not working

旧巷老猫 提交于 2019-12-02 17:43:31

问题


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:

Models:

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

Controller:

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

View:

@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>

回答1:


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

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

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




回答2:


The currency annotation can be used. However it is just telling MVC which display or editor template to use. As We said current template uses the system currency. You would have to provide custom editor template or display template and some other way to determine the currency symbol to display.Look here at how to provide your own implementations

Try using this

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

Example

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

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

Check here for more details



来源:https://stackoverflow.com/questions/39600688/data-annotation-for-currency-format-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!