How to go through the FormCollection form to check if text fields have values?

I have a search page that has 6 text fields that I pass as a FormCollection to the action in the controller. I do not want to search for entries if there are no values โ€‹โ€‹in the text files.

Is there a way to iterate over all the text fields in a FormCollection to check which ones have values โ€‹โ€‹in them?

I am a college student and this project is part of my summer program. I understand that this is a question for beginners :) Thank you!

+4
source share
2 answers

You can execute the FormCollection as follows:

 foreach( string key in forms.Keys ) { ... } 

However, note that the browser only sends you names and values. It does not send you input types, so you have no way to check if the value is a flag if you do not know all the flag names in advance. But if that is the case, you donโ€™t need to go in cycles - just pull them out of the collection by name.

+9
source
 List<string> list = new List<string>(); for(int i= 0; i< form.AllKeys.Count(); ++i) { list.Add(form.Get(i)); } 

This will give you all the actual values โ€‹โ€‹for each key.

+2
source

Source: https://habr.com/ru/post/1311251/


All Articles