if else statement in Razor is not functioning

前端 未结 4 898
轻奢々
轻奢々 2021-02-05 05:01

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

 @foreach (var item in Model)
    {
        
                  


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-05 05:06

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

    @foreach (var item in Model)
        {
            
                @Html.DisplayFor(modelItem => item.Name)
                
                
                    @if (item.Amount == null)
                    {
                        @Html.Raw("--")
                    }
                    else
                    {
                        String.Format("{0:0.##}", item.Amount);
                    }
                
            
    
        }
    

提交回复
热议问题