Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 某些字符串的输出_C#_Asp.net_Web - Fatal编程技术网

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>