views:

198

answers:

2

Hi,

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

I ve 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 recordCount == 0 ? "displayNone" : "displayInherit";
}
+2  A: 

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
<% }
Ryan Rivest
Thanks for your answer Ryan but as far as i ve known and read and experienced when you start to write if in your view you should better do it with an HtmlHelper extension method. They say the view must be dumb
Barbaros Alp
No problem! I still feel that the view is dumb in this example. It's no different than writing a foreach loop in a list action's view. Do you create an HtmlHelper extension method every time you encounter a control structure in your views?
Ryan Rivest
I'm with Ryan in this case - this is display logic not model logic so at some point you have to make a decision. Using an if, as suggested, means you send just what you need over the wire rather than lobbing up two divs one of which isn't rendered. Your other option is to have a different view - but that would depend to a certain extent on what else you want on the page.
Murph
Ok, i see. Thank you for your answers. I just got curious how others handle this situation.
Barbaros Alp
I like Murph's different view idea better. You could put the Model.Count check in your action, and if it's 0, return a NoRecordsFound view, otherwise carry on as normal. That would eliminate putting the logic in your view in this case.
Ryan Rivest
Yes it is worth trying
Barbaros Alp
I have picked up the if else.. by that way i get rid off two extension methods and css classes. Thanks RyanNote: It is better to think not complex sometimes :))
Barbaros Alp
+1  A: 

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.

Hapkido