简体   繁体   中英

Display a value in HTML using MVC

I have a list held by Session's variable. Suppose: 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).

Here is what I tried in my HTML code in order to display the number:

@(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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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