skip to Main Content

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

2

Answers


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

    @if (((string[])ViewBag.Funds).Contains("a"))
    {
    
    }
    
    Login or Signup to reply.
  2. @if (ViewBag.Funds.Any(s=>s.Contains("a"))))
    {
                   
    }
            
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search