if else statement in Razor is not functioning

I am using an if else in Razor view to check for null value like this:

 @foreach (var item in Model)
    {
        <tr id="@(item.ShopListID)">
            <td class="shoptablename">@Html.DisplayFor(modelItem => item.Name)
            </td>
            <td class="shoptableamount">
                @if (item.Amount == null)
                {
                    Html.Display("--");
                }
                else
                {
                    String.Format("{0:0.##}", item.Amount);
                }
            </td>
        </tr>

    }

However, no matter my model amount is null or having a value, the html rendered do not contain any value in the amount.

I wonder why this is happening. Any idea?

Thanks...

EDIT:

Decided to did it in controller:

   // Function to return shop list food item amount
    public string GetItemAmount(int fid)
    {
        string output = "";

        // Select the item based on shoplistfoodid
        var shopListFood = dbEntities.SHOPLISTFOODs.Single(s => s.ShopListFoodID == fid);

        if (shopListFood.Amount == null)
        {
            output = "--";
        }
        else
        {
            output = String.Format("{0:0.##}", shopListFood.Amount);
        }
        return output;
    }

and call at View like:

 <td class="shoptableamount">
                @Html.Action("GetItemAmount", "Shop", new { fid = item.ShopListFoodID })
            </td>

Answers


You have to use the @()

            @if (item.Amount == null)
            {
                @("--");
            }
            else
            {
                @String.Format("{0:0.##}", item.Amount)
            }

As noted in the comments and other answers, the Html.Display is not for displaying strings, but for displaying data from the ViewData Dictionary or from a Model. Read http://msdn.microsoft.com/en-us/library/ee310174%28v=VS.98%29.aspx#Y0


I think you want to display "-----" if amount is null.

@foreach (var item in Model)
    {
        <tr id="@(item.ShopListID)">
            <td class="shoptablename">@Html.DisplayFor(modelItem => item.Name)
            </td>
            <td class="shoptableamount">
                @if (item.Amount == null)
                {
                    @Html.Raw("--")
                }
                else
                {
                    String.Format("{0:0.##}", item.Amount);
                }
            </td>
        </tr>

    }

That's because you are using the Display() method incorrectly. The overload you are using is Display(HtmlHelper, String). If you are looking for "--" to be the text, you should use something like:

@Html.Label("--");

There are actually two other ways to display text from a code block in razor besides the suggested @(""), using a <text> tag and it's shorthand @:

    @{
        @("--")
        <text>--</text>
        @:--
    }

The code above will display -- three times.


Need Your Help

Why use else in try/except construct in Python?

python exception-handling try-catch

I am learning Python and have stumbled upon a concept I can't readily digest: the optional else block within the try construct.

Connecting a mapped drive letter in Windows service

delphi windows-services unc network-drive mapped-drive

I'm writing a Windows service (Delphi XE2) that calls functions in a 3rd party COM library. The core of the COM library is quite old and requires that a drive letter is mapped to the location of its

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.