首页中有一个代码,可以从数据库中输出字符串:
@foreach (var post in Model.Posts)
{
<a asp-controller="Home" asp-action="Post" asp-route-id="@post.Id" >
<span class="title">
@post.Title,
@post.Body,
@post.Category
</span>
</a>
}所有字符串都正确输出。每个字符串都有一些变量(在表dbo.Posts和模型文件Post.cs中)
public string Title { get; set; } = "";
public string Body { get; set; } = "";
public string Category { get; set; } = "";我希望程序可以在页面上输出表格,只有字符串组成的变量“类别”的某些值,在其他情况下字符串不输出。这怎么可能呢?
发布于 2020-01-09 14:04:18
在将您的帖子集发送到视图之前,在服务器端对其进行过滤。例如:
viewModel.Posts = GetPosts().Where(p => p.Category == "certain value");发布于 2020-01-09 14:33:40
也许,你可以试试:
<table>
<tr>
<th></th>
<th>
Title
</th>
<th>
Body
</th>
<th>
Category
</th>
</tr>
<% foreach (var post in Model.Posts) { %>
<tr>
<td></td>
<td>
<at sign>post.Id
</td>
<td>
<at sign>post.Title
</td>
<td>
<at sign>post.Body
</td>
</tr>
<% } %>
</table> https://stackoverflow.com/questions/59657904
复制相似问题