C# 某些字符串的输出
主页中有一个从数据库输出字符串的代码:C# 某些字符串的输出,c#,asp.net,web,C#,Asp.net,Web,主页中有一个从数据库输出字符串的代码: @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,
@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>
}
我想,该程序可以在表中的页面上输出只有字符串组成的变量“类别”的某些值,在其他情况下字符串是不输出。如何实现这一点?在将帖子集发送到视图之前,先在服务器端过滤帖子集。例如:
viewModel.Posts = GetPosts().Where(p => p.Category == "certain value");
或许,您可以尝试:
<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>
标题
身体
类别
邮政编码
职务
邮政机构
使用表格
、tr
和td
呈现表格结构。。。用户if(condition)
检查类别的值我做了一个条件,它工作正常。然而,代码的风格并不好。我如何优化@foreach(Model.Posts中的var post){@Antonieru为什么不在服务器端对其进行过滤,正如我在回答中所显示的那样?
<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>