1

我正在使用 MS WebMatrix 从数据库中获取的数据创建动态 CSHTML 页面。我正在使用剃刀语法。

我有数据返回到我的 CSHTML 页面,但它有尾随数字等。例如,我想将“123.4568”格式化为“$123.46”

如何让结果以我需要的格式显示?我还想在达到目标时更改文本颜色等。

4

1 回答 1

1

您可以使用列构造函数的格式可选参数设置 WebGrid 中值的格式。

您可以尝试这样的事情,如果值 > 50,发票的颜色为红色,否则为蓝色

<html lang="en">
    <head>
        <style>
            .column1 {color: red; font-weight: bold;}
            .column2 {color: blue;}
        </style>
    </head>
    <body>
        <p>@stot1.GetHtml(
            columns:stot1.Columns(
                stot1.Column(
                    columnName:"Invoices",
                    format:@<text>
                                <span class=@(item.Invoices > 50 ? "column1" : "column2")>
                                    @item.Invoices.ToString("C")
                                </span>
                            </text>
                )
            )
        )</p>
    </body>
</html>

但我认为没有理由在您的情况下使用 WebGrids。

WebGrid 帮助器从支持分页和排序的数据库中呈现数据,但您的查询只返回一个值,您不需要其中任何一个。

已编辑

更好的解决方案是使用 QueryValue 方法查询表,该方法返回单个标量值,并在没有 WebGrid 的情况下显示该值。

以下是唯一发票的示例

@{
    var db = Database.Open("YourTable");
    var itot = db.QueryValue("SELECT SUM(Subtotal) AS Invoices FROM Salesord_hdr where Orderdate = 41190 and Invoicecount >= 1");
}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <style>
            .column1 {color: red; font-weight: bold;}
            .column2 {color: blue;}
        </style>
    </head>
    <body>
        <p>Invoices: 
            <span class=@(itot > 50 ? "column1" : "column2")>@itot.ToString("C")</span>
        </p>
    </body>
</html>
于 2012-10-11T10:10:46.317 回答