Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6947197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:39:47+00:00 2026-05-27T13:39:47+00:00

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

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:39:48+00:00Added an answer on May 27, 2026 at 1:39 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to nest a foreach within an if/else using Razor, but am having
Anyone else get this or suffer from this? I am using Vista and VS
Im using the following JS <a href=# onClick=if($(this).next('div').css('display') == 'none') { $(this).next('div').show('fast'); } else
I'm using this: jQuery('.class1 a').click( function() { if ($(.class2).is(:hidden)) { $(.class2).slideDown(slow); } else {
I am building wizard step demo application with MVC3 and using razor view engine
I'm using the Razor view engine with MVC 3 and I'm trying to make
Here's the situation: ASP.NET MVC 3 application using Razor as the view engine. Works
I am using jQuery and ASP.NET MVC 3 with the razor view engine .
I was using this if (ret = 1) iconType = new google.maps.MarkerImage('@Url.Content(~/Content/images/image.png)'); else if
I just started using Razor instead of the WebForms-ViewEngine. Now in my Razor-View i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.