Question

In Razor view I can define an array easily

@{  
    ViewBag.Title = "_Navigation";
    var foo = new string[0];
}

But if I want to define the elements of the string, the natural way would be

@{  
    ViewBag.Title = "_Navigation";
    var foo = new string[]{"foo", "bar"};
}

The latter doesn't work. Can you tell me why and how to define my foo-array properly?

============ EDIT ============

you are absolutely right, there is nothing wrong. The code i actually used was

@{  
    ViewBag.Title = "_Navigation";
    var action = @ViewContext.RouteData.GetRequiredString("action");  
    var foo = new string[]{"foo", "bar"};
}

and somehow the @ sign before my line in question coused the compiler to complain. I don't know why the @ was there - code was not from me... ok, it works now.

Thanks anyway.

Was it helpful?

Solution

There's nothing wrong with how you're defining the array.

@{
    ViewBag.Title = "_Navigation";
    var foo = new string[]{"foo", "bar"};
}
...
@foreach(var f in foo)
{
    <span>@f</span>
}

Prints

<span>foo</span><span>bar</span>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top