简体   繁体   English

使用MVC在HTML中显示值

[英]Display a value in HTML using MVC

I have a list held by Session's variable. 我有一个Session变量保存的列表。 Suppose: Session["SOF"] . 假设: Session["SOF"] Within this list, there are several items. 在此列表中,有几个项目。 Suppose each item is an integer. 假设每个项目都是一个整数。 Let's say I want to find the item which has the value 9 and then display the item's value in HTML (this thing is clearly stupid but it is just example). 假设我要查找值为9的项目,然后以HTML显示该项目的值(这显然很愚蠢,但这只是示例)。

Here is what I tried in my HTML code in order to display the number: 这是我在HTML代码中尝试显示的数字:

@(List<int>(Session["SOF"]).FirstOrDefault(x => x.value == 9).ToString());

obviously it didn't work. 显然它没有用。 So how can I do it? 那我该怎么办呢?

I would use foreach in this case 我会在这种情况下使用foreach

Try this: 尝试这个:

@foreach(var item in Session["SOF"])
{
    if(item == 9)
    {
        <span>@item.ToString()</span>
    }
}

Maybe casting to a List<int> will be needed for Session["SOF"], but give it a try. Session [“ SOF”]可能需要强制转换为List<int> ,但请尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM