Hide table or grid and display "No records found" message in ASP.NET MVC
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>
精彩评论