Let's suppose I have an array of objects myObjects whose string properties are Alpha, Beta, Gamma, ...

In my View I could display the content with Razor like this

<ul>
@foreach(obj in myObjects){
 <li>@obj.Alpha</li>
 <li>@obj.Beta</li>
 <li>@obj.Gamma</li>
 ...
}
<ul>

But if I want to compact the code, passing an array with the propery names (via ViewBag, for example), and try something like

<ul>
@foreach(obj in myObjects){
 @foreach(name in ViewBag.Names)    
    <li>@obj.@name</li>
    }
}
<ul>

Razor does not interpolate that as object.property but would simply output

myObjects.Alpha
myObjects.Beta
myObjects.Gamma
....

I'm quite sure there are a million ways to get the same result, but since something like that would work quite easily in PHP or JavaScript and I'm new to Razor I'd like to know more about its features. I have alreaty tried quite a few things, such as String.Concat, String.Format, and various combination of @ and parentheses, but no luck.

Thanks

Related posts

Recent Viewed