首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >某些字符串的输出

问某些字符串的输出
EN

Stack Overflow用户
提问于 2020-01-09 13:48:31
回答 2查看 52关注 0票数 0

首页中有一个代码,可以从数据库中输出字符串:

代码语言:javascript
复制
@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中)

代码语言:javascript
复制
public string Title { get; set; } = "";
public string Body { get; set; } = "";
public string Category { get; set; } = "";

我希望程序可以在页面上输出表格,只有字符串组成的变量“类别”的某些值,在其他情况下字符串不输出。这怎么可能呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-09 14:04:18

在将您的帖子集发送到视图之前,在服务器端对其进行过滤。例如:

代码语言:javascript
复制
viewModel.Posts = GetPosts().Where(p => p.Category == "certain value");
票数 0
EN

Stack Overflow用户

发布于 2020-01-09 14:33:40

也许,你可以试试:

代码语言:javascript
复制
<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> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59657904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档