Richa
Richa

Reputation: 3289

Reading Session value in View MVC

I have a Session in my Controller as follow

Session["Fulldata"] = data;

Which stores data like Name,Id,City etc;

It stores multiple rows of such data.

How how do I access or check if any row has particular Name in it, in the View?

eg :

@if(Session("Fulldata").has("ABC"))
{
   //Do Something
}

I want to check Name for each row in Session. Any help will be appreciated

Upvotes: 2

Views: 773

Answers (1)

Adil
Adil

Reputation: 148110

First you need to cast the Session("Fulldata") back to you type as session store your collection as object type.

List<CustomClass> data = (List<CustomClass>)Session("Fulldata");

If data is a collection you can use linq Enumerable.Any to Search

@if(data.Any(d=>d.YourAttr1 == 'ABC' || d.YourAttr2 == 'ABC'))
{
   //Do Something
}

As a additional note please do not use session unnecessarily especially for big data as session will need space on your server and as user/session increase it could adversely effect the performance.

Upvotes: 4

Related Questions