2

I have an ASP.NET MVC App. I am building an HTML table using razor syntax. The page model is defined as

@model IEnumerable < DealView.Models.deal >

and the model has a property

public string price { get; set; }

which can be a number or null.

I am trying to get the textbox for to show comma's (ie 1,000,000) or even better currency ($1,000,000). At the moment I am just getting (1000000) using

@foreach (var item in Model)
    {
        <tr>
            ...
            <td>@Html.TextBoxFor(modelItem => item.price, new { id = string.Format("
                   {0}_price", item.ID) })</td>
            ...
        </tr>
    }

I have tried item.price.asint() but think the null instances causes problems. Any advice is appreciated. I am not married to the TextBoxFor if there is a better helper function to use.

tereško
  • 58,060
  • 25
  • 98
  • 150
wbm
  • 155
  • 1
  • 2
  • 13
  • 1
    The first thing I would advice is to change your datatype to `decimal` and then look here : http://stackoverflow.com/questions/10741190/currency-formatting-mvc . As for the nullability: you probably are better of to make sure the model contains 0 values instead of null, or if you want to keep the null values, use `decimal?` – Stephen Jan 28 '14 at 23:29
  • The problem is the data is held as string in the xml source which I cannot amend. I am reading it into the property like this – wbm Jan 28 '14 at 23:52
  • price = xml.Element("price").Value ?? "0" – wbm Jan 28 '14 at 23:52
  • (sorry, new to this) but it doesn't like if I change the type to decimal because it recognises the source as string. Any advice how to cast it is also appreciated – wbm Jan 28 '14 at 23:53

2 Answers2

2

If you can change the type I'd use a nullable numeric type (int?). Then you can use the built-in formats.

price.GetValueOrDefault(0).ToString("C0")

If you can't change the string type then write a custom HtmlHelper extension to format your strings.

public static class HtmlHelperExtensions
{
    public static string FormatCurrency(this HtmlHelper helper, string val)
    {
        var formattedStr = val;  // TODO: format as currency
        return formattedStr;
    }
}

Use in your views

@Html.FormatCurrency(price)
Jasen
  • 14,030
  • 3
  • 51
  • 68
2

You could firstly parse the string so the view model is a strongly typed number (int, decimal, etc). I'll use a nullable decimal.

public ActionResult MyAction()
{
    string theThingToParse = "1000000";
    ViewModel viewModel = new ViewModel();

    if(!string.IsNullOrEmpty(theThingToParse))
    {
        viewModel.Price = decimal.parse(theThingToParse);    
    }

    return View(viewModel);
}

For simplicity you could apply the following annotation on the property in your view model:

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

Now if you use EditorFor in your view the format specified in the annotation should be applied and your value should be comma separated:

<%= Html.EditorFor(model => model.Price) %>
David Spence
  • 7,999
  • 3
  • 39
  • 63