1

I am using MS WebMatrix to create a dynamic CSHTML page from data obtained from a database. I am using the razor syntax.

I have data being returned to my CSHTML page, but it has trailing digits etc. For example I want to format "123.4568" into "$123.46"

How do I get the result to display in the format I need? I also want to change text colour when a target is reached, etc.

2
  • You could use the c string format - see stackoverflow.com/questions/7087495/… Commented Oct 11, 2012 at 1:09
  • Sort of helpful, thanks. I can't get the code to accept my formatting so I am using the wrong syntax (again!) Here's the string as it stands: var itot = db.Query("SELECT SUM(SUBTOTAL) as Invoices from dbo.SALESORD_HDR where ORDERDATE = 41190 and INVOICECOUNT >= 1"); var stotl = new WebGrid(source: itot); var qtot = db.Query("SELECT SUM(SUBTOTAL) as Quotes FROM dbo.SALESORD_HDR where ORDERDATE > 41190"); var qtotl = new WebGrid(source: qtot); and then I render the results in the html thus: <p> @qtotl.GetHtml() @stotl.GetHtml() </p> Commented Oct 11, 2012 at 1:35

1 Answer 1

1

You can set the format of values in a WebGrid using the format optional argument of the column constructor.

You could try something like this, where the color of Invoices is red if value > 50 or blue if not

<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>

but I see no reason to use WebGrids in your situation.

The WebGrid helper renders data from a database with support for paging and sorting, but your query returns only one value and you don't need either of them.

Edited

A better solution is to query the table with the QueryValue method, which returns a single scalar value, and display the value without the WebGrid.

In the following an example for the only Invoice

@{
    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>
Sign up to request clarification or add additional context in comments.

1 Comment

OK, so how do I get it to display just the single result I am needing then? If i just insert the @var i get a string of code text. Sorry to be so thick, but what do I do to get the data to display how I want? And would I be better to use a pure HTML page instead of CSHTML and Razor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.