I have the following for loop written in PHP:
$activesFilters = 0;
foreach ($_GET as $key => $value) {
if(!empty($value)) $activesFilters++;
}
if(isset($_GET['Filter'])) { $activesFilters = $activesFilters - 1; }
if(isset($_GET['CustomFilter'])) { $activesFilters = $activesFilters - 1; }
if(isset($_GET['showArchived'])) { $activesFilters = $activesFilters - 1; }
and I want to convert it over to ASP.NET C# Razor.
So far I have:
int activeFilter = 0;
@foreach(Request.QueryString as key in value) {
if(!empty(value)) activeFilters+1;
}
@if(Request.QueryString['Filter']) activeFilters = activeFilters - 1;
@if(Request.QueryString['CustomFilter']) activeFilters = activeFilters - 1;
@if(Request.QueryString['showArchived']) activeFilters = activeFilters - 1;
It's the foreach
loop I'm confused about.
As I don't understand how to handle the key => value
in C#
I've written the above based on: http://msdn.microsoft.com/en-us/library/ttw7t8t6(v=vs.90).aspx but it doesn't show any examples for handling the key + value part.
UPDATE:
Based on some help from below:
@int activeFilters = 0;
@foreach (string key in Request.QueryString)
{
string value = Request.QueryString[key];
if(String.IsNullOrEmpty(value)){
return activeFilters+1;
}
}
However it says activeFilters
does not exist in the current context. How do I expose it to the foreach
loop in C#? As this is fine in the PHP world.