对不起,我真的不知道怎么称呼它。 我有一个显示公告的网页。我似乎无法弄清楚是否有任何公告,如果有显示说"没有公告"。
我尝过这样的话:
Because my first approach was to do that inside an Infrastructure layer/lib which listen to the events from the job layer
但这不起作用。我在哪里处理这些事情?视图/控制器?
查看:
if(db.Announcements.toArray().length == 0){
return View(null);
}
控制器:
@model IEnumerable<Remake.Models.Announcement>
@{
ViewBag.Title = "Announcements";
}
<h2>Announcements</h2>
@if (User.Identity.IsAuthenticated)
{ <p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
<b> @Html.DisplayNameFor(model => model.Title)</b>
</th>
<th>
@Html.DisplayNameFor(model => model.Content)
</th>
<th width="10%">
@Html.DisplayName("Date")
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<b> @Html.DisplayFor(modelItem => item.Title)</b>
</td>
<td>
@Html.DisplayFor(modelItem => item.Content)
</td>
<td>
<b>@Html.DisplayFor(modelItem => item.PostDate)</b>
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.AnnouncementId }) |
@Html.ActionLink("Comments", "Details", new { id = item.AnnouncementId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.AnnouncementId })
</td>
</tr>
}
</table>
}
else
{
<p>Please sign in to create an Announcement</p>
}
答案 0 :(得分:1)
由于您的模型定义为IEnumerable<Announcement>
,因此您只需使用Any()
检查其是否为空:
@if (Model.Any())
{
// show announcements
foreach (var item in Model)
{
// ...
}
}
else
{
// show message when empty
<p>No announcements</p>
}
请参阅MSDN