开发者

If statement problem in MVC View

I am trying to show rows of data in a table with the following code. In case there is no data for the table I want a simple message to be printed. My code does print the rows of data as intended when there is data to show but when the list is empty it prints the table headings. How can I get rid of the headings and print the message instead?

<fieldset>

    <legend>Department Membership</legend>
<% if(Model.departmentsDisplayCheck) {%>
<table>
    <tr>         
开发者_StackOverflow社区        <th>Name</th>
        <th>Type</th>
        <th>Age</th>
        <th>Gender</th>
        <th>Status</th>
    </tr>

<% foreach (var dep in Model.departmentsList){ %>

    <tr>
        <td><%: Html.ActionLink(dep.Name, "Details", "Department", new { id=dep.DepartmentID}, null) %></td>
        <td><%: dep.Type %></td>
        <td><%: dep.Age %></td>
        <td><%: dep.Gender.ToString() %></td>
        <td><%: dep.Status %></td>
    </tr>
<% } %>
<% } %>
<% else { %>
<p><%: "You are not currently a member of any Department." %></p>
<% } %>
</table>

</fieldset>


First off these kinds of checks don't belong in a View. Instead you should have a property on your controller called something like DisplayDepartmentList and then set it using your checks (and any other business logic that may come up) there.

Then instead you are doing this check..

<% if(Model.DisplayDepartmentList) 


<% if(Model.departmentsList != null && Model.departmentsList.Count > 0) {%>


Are you sure that the model is null and not just empty? If it's coming back empty then you'd get the observed behavior.

Try changing it to

<% if(Model.departmentsList != null || Model.departmentList.Count == 0) {%>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜