How to display a parenthese in c# code in Razor

how to write c# code in mvc view
razor syntax cheat sheet
razor html helpers
razor if statement with html inside
razor variable in html
razor escape @
razor pages
cshtml @ symbol

I'm strugaling to display a paranthese in a "if" block in Razor.

In a single page where I'm displaying some few information I would like to display a text which should be "Name of something (number)". Where I want "(number)" to only been displayu if the nember is different from zero.

Then I made this code:

<div class="jumbotron">
<h1>Votre commande au @Model.Resto_Name</h1>

<hr />
<h2> Menu </h2>

<div class="container">
    <div class="col-sm-6">
        @foreach (var item in Model.ListOfProposedItems)
        {
            <div class="card" style="width: 18rem;">
                <img class="card-img-top" src="@Url.Action("RenderItemPhoto", "Menu", new { ItemId = item.ItemId })" alt="Card image cap">
                <div class="card-body">
                    <h2 class="card-title">@item.Name.ToString()</h2> <h4>@if (item.Quantity != 0){ ( @item.Quantity )} </h4>
                    <p class="card-text">@item.Description.ToString()</p>
                    <a href="@Url.Action("AddItemToOrder", "Order", new { ItemId = item.ItemId, OrderId = Model.OrderId })" class="btn btn-primary">Ajouter</a>
                </div>
            </div>
        }
    </div>
</div>

Then I have the issue where Razor interpret my brakets as code but not text to display.

To display paranthesis within Razor code, you can do the following

<h4>@if (item.Quantity != 0) 
    { 
    @: ( @item.Quantity ) 
    }
</h4>

@: allow you to explicitly indicate that the line of content should be treaded as a content block. You can read more on "@:"here

Difference Between Razor View Engine and ASPX View Engine, Which is more faster between ASPX view engine and Razor view engine? Server code can create dynamic HTML content on the fly, before it is sent to the browser. Seen from the browser, the HTML generated by server code is no different than static HTML content. ASP.NET web pages with Razor syntax have the special file extension cshtml (Razor using C#) or vbhtml (Razor using VB).

You can wrap your parentheses in text tags

<text>(</text>

Part 3: Views and ViewModels, Razor view with visual basic syntax has .vbhtml file extension and C# syntax has You can write multiple line of server side code enclosed in braces @{ } . ASP.NET Razor Razor Intro Razor Syntax Razor C# Variables Razor C# Loops Razor C# Logic Razor VB Variables Razor VB Loops Razor VB Logic ASP Classic ASP Intro ASP Syntax ASP Variables ASP Procedures ASP Conditionals ASP Looping ASP Forms ASP Cookies ASP Session ASP Application ASP #include ASP Global.asa ASP AJAX ASP e-mail ASP Examples ASP

<h4>@(item.Quantity != 0? $"({item.Quantity})" : "")</h4>

When you type @() everything inside () will be executed as C# code

razor syntax reference for ASP.NET Core, Learn about Razor markup syntax for embedding server-based code into Inspect the Razor C# class generated for a view; View lookups and Explicit Razor expressions consist of an @ symbol with balanced parenthesis. Intellisense: Razor syntax supports statement completion within Visual Studio. Now, let's learn how to write razor code. Inline expression. Start with @ symbol to write server side C# or VB code with Html code. For example, write @Variable_Name to display a value of a server side variable. For example, DateTime.Now returns a current date and time.

Razor Syntax, NET Core MVC you can use Razor syntax to write C# code in the view. You can resolve this by using parentheses as shown in the following example to mark​  0 How to display a parenthese in c# code in Razor Jan 19 '19 0 Test WebAPI on localhost but note resolving the address Feb 19 '19 0 Xamarin - How to display en Image from an Object property Feb 24 '19

ASP.NET Razor Syntax, The Razor syntax is based on the C# programming language, and that's For details, see the appendix Visual Basic Language and Syntax. A code block includes one or more code statements and is enclosed in braces. Razor code blocks. Razor code blocks start with @ and are enclosed by {}. Unlike expressions, C# code inside code blocks isn't rendered. Code blocks and expressions in a view share the same scope and are defined in order: @{ var quote = "The future depends on what you do today.

Professional C# 7 and .NET Core 2.0, With Razor you need to differentiate statements that return a value and methods Razor syntax, the engine automatically detects the end of the C# code when it finds You can resolve this by using parentheses as shown in the following example to There's another way to pass data to a view—pass a modell to the view. Is there a way to get the value of a variable in the Razor Page code behind page (cshtml.cs) from the cshtml page? The cshtml page is what's shown in the browser. The user clicks a link or submits a form to send data from the browser to the code behind on the server.

Comments
  • Sorry this does not work. Now once executed, browser says it does not find de « } » of "foreach" block ...