开发者

Hide table or grid and display "No records found" message in ASP.NET MVC

开发者 https://www.devze.com 2022-12-16 09:21 出处:网络
What is the best practice for hiding a custom grid or a table or a div and display a \"No Records Found\" message when there are no records.

What is the best practice for hiding a custom grid or a table or a div and display a "No Records Found" message when there are no records.

I have come up with this idea.

<div class="<%= Html.IsVisible(Model.Count)">
    ...
    ..
    ..
</div>   

.displayNone {display:none;} .displayInherit {display:inherit;}

public static string IsVisible(this HtmlHelper helper,int recordCount)
{
     return re开发者_如何学GocordCount == 0 ? "displayNone" : "displayInherit";
}


Your solution would work fine, but I think you might be overthinking it a little :)

This would work perfectly fine:

<% if (Model.Count == 0) { %>
    No Records Found
<% } else { %>
    // do something to show the Model information here
<% }


Make the if in the controller?

if Model.Count == 0 display the "EmptyView" else show the GridView

Empty view could be made generic to be use from several objects.


Following solution is better for razor engine

@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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号