0

I have searched a lot. Perhaps I am looking in the wrong places...

I have a product list and cost price within the product list. An extract from the Model:

    [Display(Name = "Cost Price")]
    [DataType(DataType.Currency)]       
    public decimal CostPrice { get; set; }

I am using Datatype at this level and it works fine.

I then reference the product model from a Kitset model. A kitset being a collection of products to manufacture a complete solution. A real world example could be tyre, wheel, wheel nuts and hub cap.

 public class ProductKitset
{
    public int ProductKitsetID { get; set; }

    [Display(Name ="Kitset Name")]
    public int ProductKitsetNameID { get; set; }

    public decimal Quantity { get; set; }

    [Display(Name = "Product")]
    public int ProductID { get; set; }

    public Product Product { get; set; }

    public ProductKitsetName ProductKitsetName { get; set; }
}

Then as some people want quotes, and quotes may include one or more Kitset, I have a third model QuoteToKitset:

   public class QuoteToKitset
{
    public int QuoteToKitsetID { get; set; }

    public int QuoteID { get; set; }

    public int ProductKitsetID { get; set; }

    public Quote Quote { get; set; }

    public ProductKitset ProductKitset { get; set; }
}

At the end of this chain I then have a ViewComponent. The ViewComponent is returning the listing of products included in the kitset within the Quote. The purpose is so the person preparing the quote can see what is in the kitset in case they need to add another item. Going back to our wheel example, perhaps a disk brake rotor would be required as well.

THis works fine as far as it goes and is returning the results I want. For completeness the viewComponent:

public class ProductKitsetsViewComponent :  ViewComponent
{
    private readonly Eva804Context _context;

    public ProductKitsetsViewComponent(Eva804Context context)
    {
        _context = context;
    }

    public IViewComponentResult Invoke(int id)
    {
        var productKitset = from p in _context.ProductKitset
                   .Include(p=>p.Product)
                   .Where(p => p.ProductKitsetNameID == id )                       
                   select p;

        return View(productKitset);
    }

}

Then in the default view for the ViewComponent I have:

  @foreach (var p in Model)
    {
        <tr>
            <td>

                @p.Product.ProductCode
            </td>
            <td>

                @p.Quantity
            </td>
            <td>
                @p.Product.ProductDescription.Substring(0, Math.Min(p.Product.ProductDescription.Length, 38))
            </td>

            <td>

                @p.Product.CostPrice
            </td>

This is working fine as I said. Except for the formatting on Cost Price. As I read this, and I am still learning my way into this complex world, the Cost Price formatting is set by the DataType in the product Model.

In the real world the format is not being reproduced in the ViewComponent.

Why is the datatype being ignored? How can I fix this? Is there something incorrect in my thinking where I have missed how something works here?

BitLost
  • 175
  • 1
  • 1
  • 8
  • What format are you getting ? What's the output ? – Mads... Apr 25 '17 at 08:05
  • Sorry didn't notice this comment. I am simply getting the native decimal format, with no formatting applied. I am not sure why this is happening but the below fixed it. – BitLost Apr 26 '17 at 09:17

1 Answers1

0

There are a few ways to do that.

@p.Product.CostPrice.ToString("C")

where the format C goes for Currency. If you want to specify the format (Dollar, Euro, Pound...) you may refer to this.

Another way is to specify the DisplayFormat attribute:

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

or

[DisplayFormat(DataFormatString = "${0:#,###0.00}")]

according to this SO question.

Community
  • 1
  • 1
Kroneaux Schneider
  • 264
  • 1
  • 2
  • 13
  • @p.Product.CostPrice.ToString("C") Works. For interest I tried the DisplayFormat in the model, however this fails when passed through this process to the ViewComponent. – BitLost Apr 25 '17 at 20:55