代码之家  ›  专栏  ›  技术社区  ›  Barbaros Alp

在ASP.NET MVC中隐藏表格或网格并显示“未找到记录”消息

  •  2
  • Barbaros Alp  · 技术社区  · 16 年前

    隐藏自定义网格、表或div并在没有记录时显示“未找到记录”消息的最佳做法是什么。

    我想出了这个主意。

    <div class="<%= Html.IsVisible(Model.Count)">
        ...
        ..
        ..
    </div>   
    
    .displayNone {display:none;} .displayInherit {display:inherit;}
    
    public static string IsVisible(this HtmlHelper helper,int recordCount)
    {
         return recordCount == 0 ? "displayNone" : "displayInherit";
    }
    
    3 回复  |  直到 11 年前
        1
  •  3
  •   Ryan Rivest    16 年前

    您的解决方案会很有效,但我认为您可能有点想得太多了:)

    这将非常有效:

    <% if (Model.Count == 0) { %>
        No Records Found
    <% } else { %>
        // do something to show the Model information here
    <% }
    
        2
  •  1
  •   Hapkido    16 年前

    如果Model.Count==0,则显示“EmptyView”,否则显示GridView

        3
  •  0
  •   Akshay Mishra    8 年前

    以下解决方案更适合剃须刀引擎

    @model IEnumerable<WebApp.Models.ArticleViewModel>
    
    <div id="answers">
        @if (Model.Count() == 0)
        {
            <div class="question-summary">
                <p>No answer found</p>
            </div>
        }
        else
        {
            foreach (var item in Model)
            {
                <div class="question-summary">
                     @Html.Raw(item.Body)
                </div>
            }
        }
    
    </div>