I need to select multiple fields into an enumerator to run a foreach in my Razor web app.
In the controller, I have:
...
cols = (from b in a.RefTable select new {b.Col1,b.Col2,b.Col3}),
...
It returns the values correctly when I use:
@foreach(var col in @item.cols)
{
@col
}
However, the representation on the page is:
{ Col1 = col1Value, Col2 = col2Value, Col3 = col3Value }
Two things I want to do:
Only show the column values on the page without being comma separated (will be separated onto each line within a dataTable field), and then not to show any value if the "col2Value", for example, is blank.
Edit: Created a new ViewModel to resolve this, e.g.:
public class ColViewModel
{
public string Col1 {get;set;}
...
}
And replaced the Linq with:
...
cols = from b in a.RefTable select new ColViewModel{Col1 = b.Col1, ...},
...
Then, using the example from @Sacrilege below, I'm able to get the strings in the format I need them in.