1

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>
nam
  • 21,967
  • 37
  • 158
  • 332

2 Answers2

4

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

  • 1
    Is there a workaround for not using custom model binder. Instead of `float?`, I tried `decimal?` and `int?` but in both cases I got the same error at `@Html.TextBoxFor(r => r[i].SaleAmount)` line of the View: `FormatException: Input string was not in a correct format.` I deleted the database and re-ran `add-migration` and `update-database` commands on Package Manager console before changing the float to decimal and int. – nam Sep 21 '16 at 01:07
  • You cannot use `TextBoxFor()` as I explained. And it does not matter if you use `float` or `decimal` or any other numeric type. If the value in your textbox (e.g. `$15,481`) cannot be converted to a numeric type then you need custom model binder (and to test it, try using `float.Parse("$15,481");` - it fails because it cannot be converted to `float`) –  Sep 21 '16 at 01:12
  • I forgot to mention that I did change `TextBoxFor` to `EditorFor` before I got the above exception. – nam Sep 21 '16 at 03:13
0

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

Community
  • 1
  • 1
MANISH KUMAR CHOUDHARY
  • 3,396
  • 3
  • 22
  • 34
  • It's not working (probably since I'm using different template). The data is displaying correctly but the format is just plain numbers. Also, please note the `data type` in SQL Server Db is `real`. I've added `Controller` and `View` in the post - in case that helps. – nam Sep 20 '16 at 21:14