Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

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

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading