You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Razor视图中如何将API返回数值格式化为货币格式

解决Razor视图中货币格式化问题

你的问题出在Razor语法解析上,之前的写法只把除法运算包裹在@()里,后面的.ToString("C2")会被当成普通文本输出,所以才出现3.ToString("C2")这种错误结果。下面提供两种可行的解决方案:

方案一:视图内直接格式化(快速实现)

把整个格式化逻辑都包裹在@()中,同时将整数转换为decimal类型避免整数除法的精度问题,示例代码如下:

@model Final_Project.Models.Root

<h2>Card Info</h2>

<table class="table" >
    <tr>
        <th>Name</th>
        <th>Variant</th>
        <th>Card Number</th>
        <th>Rarity</th>
        <th>Artist</th>
        <th>High Price</th>
        <th>Low Price</th>
        <th>Market Price</th>
    </tr>

    @foreach (var product in Model.results)
    {
        <tr>
            <td>@product.name</td>
            <td>@product.variant</td>
            <td>@product.cardNumber</td>
            <td>@product.rarity</td>
            <td>@product.artist</td>
            <td>@(((decimal)product.prices.tcgPlayer[1].high.amountInMinorUnits / 100).ToString("C2"))</td>
            <td>@(((decimal)product.prices.tcgPlayer[1].low.amountInMinorUnits / 100).ToString("C2"))</td>
            <td>@(((decimal)product.prices.tcgPlayer[1].market.amountInMinorUnits / 100).ToString("C2"))</td>
        </tr>
    }
</table>

如果觉得代码重复,也可以先在循环内定义变量简化代码:

@foreach (var product in Model.results)
{
    // 先计算转换后的价格
    var highPrice = (decimal)product.prices.tcgPlayer[1].high.amountInMinorUnits / 100;
    var lowPrice = (decimal)product.prices.tcgPlayer[1].low.amountInMinorUnits / 100;
    var marketPrice = (decimal)product.prices.tcgPlayer[1].market.amountInMinorUnits / 100;
    
    <tr>
        <td>@product.name</td>
        <td>@product.variant</td>
        <td>@product.cardNumber</td>
        <td>@product.rarity</td>
        <td>@product.artist</td>
        <td>@highPrice.ToString("C2")</td>
        <td>@lowPrice.ToString("C2")</td>
        <td>@marketPrice.ToString("C2")</td>
    </tr>
}

方案二:在模型/ViewModel层预处理(更优雅)

如果多个视图需要用到这些格式化后的价格,建议在模型或ViewModel中添加计算属性,把数据转换逻辑从视图分离出来:

1. 扩展实体类添加计算属性(如果允许修改实体)

假设你的Product类是API返回的实体,可以添加以下计算属性:

public class Product
{
    // 原有属性...
    public Prices prices { get; set; }

    // 新增计算属性
    public decimal HighPrice => (decimal)prices.tcgPlayer[1].high.amountInMinorUnits / 100;
    public decimal LowPrice => (decimal)prices.tcgPlayer[1].low.amountInMinorUnits / 100;
    public decimal MarketPrice => (decimal)prices.tcgPlayer[1].market.amountInMinorUnits / 100;
}

然后视图中直接调用属性并格式化:

<td>@product.HighPrice.ToString("C2")</td>
<td>@product.LowPrice.ToString("C2")</td>
<td>@product.MarketPrice.ToString("C2")</td>

2. 使用ViewModel映射(推荐,解耦API实体和视图)

创建专门的ViewModel来适配视图需求:

public class CardViewModel
{
    public string Name { get; set; }
    public string Variant { get; set; }
    public string CardNumber { get; set; }
    public string Rarity { get; set; }
    public string Artist { get; set; }
    public decimal HighPrice { get; set; }
    public decimal LowPrice { get; set; }
    public decimal MarketPrice { get; set; }
}

在控制器中把API返回的实体映射到ViewModel:

var apiData = // 调用API获取Root数据
var viewModelList = apiData.results.Select(product => new CardViewModel
{
    Name = product.name,
    Variant = product.variant,
    CardNumber = product.cardNumber,
    Rarity = product.rarity,
    Artist = product.artist,
    HighPrice = (decimal)product.prices.tcgPlayer[1].high.amountInMinorUnits / 100,
    LowPrice = (decimal)product.prices.tcgPlayer[1].low.amountInMinorUnits / 100,
    MarketPrice = (decimal)product.prices.tcgPlayer[1].market.amountInMinorUnits / 100
}).ToList();

return View(viewModelList);

视图引用ViewModel后,直接使用格式化:

@model List<Final_Project.Models.CardViewModel>

<!-- 表格结构略 -->
<td>@product.HighPrice.ToString("C2")</td>
<td>@product.LowPrice.ToString("C2")</td>
<td>@product.MarketPrice.ToString("C2")</td>

两种方案对比

  • 视图内格式化:适合快速实现,代码改动小,但重复代码多,不利于维护。
  • 模型/ViewModel预处理:代码更简洁,逻辑复用性强,符合MVC的职责分离原则,适合复杂场景。

内容的提问来源于stack exchange,提问作者Randall Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 15:59:50