E__
E__

Reputation: 65

How to check ViewBag contains a specific string

In controller I have:

string[] checkedBoxes 
ViewBag.Funds = checkedBoxes;

checkedBoxes is posted by a form in the view page. How do I check inside the view if ViewBag.Funds contains a specific string? I tried:

@if (ViewBag.Funds.ContainsKey("a"))
{
               
}
        
     

I got this error: RuntimeBinderException: 'System.Array' does not contain a definition for 'ContainsKey'

.Contains() also doesn't work even though I used @using System.Linq

Upvotes: 1

Views: 230

Answers (2)

Mukul Keshari
Mukul Keshari

Reputation: 505

@if (ViewBag.Funds.Any(s=>s.Contains("a"))))
{
               
}
        

Upvotes: 0

A K
A K

Reputation: 110

You have to cast the ViewBag property to the type first. Try this

@if (((string[])ViewBag.Funds).Contains("a"))
{

}

Upvotes: 1

Related Questions