简体   繁体   中英

How to display a parenthese in c# code in Razor

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

您可以将括号括在文本标签中

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

当您键入@()时, @()所有内容都将作为C#代码执行

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM