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?