How to Hide Table in MVC View when there is No Data?
asp.net-mvc, asp.net-mvc-3, linq
Solution
Simply put one `if` around table as well to check if property is not null and count of that list is greater than 0 then table should be rendered.
@if(Model != null)
{
if(Model.Values != null && Model.Values.Count != 0)
{
<table id="tableID">
<thead>
<tr>
<th>Name</th>
<th>ID</th>
<th style="width:2%;"></th>
</tr>
</thead>
<tbody>
@foreach(var value in Model.Values)
{
<tr>
<td>@value.Name</td>
<td>@value.ID</td>
</tr>
}
</tbody>
</table>
}
}
Problem
I am populating my datatable using Linq. I have hard-coded headers. And populating body columns with Linq. Following is my code. ``` <table id="tableID"> <thead> <tr> <th>Name</th> <th>ID</th> <th style="width:2%;"></th> </tr> </thead> <tbody> @if(Model.Values !=null) { foreach(var value in Model.Values) { <tr> <td>@value.Name</td> <td>@value.ID</td> </tr> } } </tbody> </table> ``` What I am thinking to do here, if there is no data table should not be visible.I thought of moving my conditional check if model is returning null prior to table creation but it will throw exception. I am fairly new to MVC. Any suggestion is appreciated. Thank you