I am using asp.mvc 4. Assumend I have a Model called Person with the fields
public class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime DateOfWorkstart { get; set; }
public int NumberOfChildren { get; set; }
public int DepartmentID { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public int ID { get; set; }
public int NameOfDepartment { get; set; }
}
In my automatically generated razor-edit-view fields are shown like this (For clearness, I only included important lines in this post)
@Html.DisplayFor(modelItem => item.FirstName)
@Html.DisplayFor(modelItem => item.SecondName)
now I would like to store the linq-lambda expressions in a list an use it later, I don't know how to do that, I need something like this:
@{
string itemsToShow = "namepart"; // this could also be "otherpart"
List <Expression<>> list = new List();
if (itemsToShow.equals("namepart")
{
list.add(modelItem => item.FirstName);
list.add(modelItem => item.SecondName);
}
else
{
list.add(modelItem => item.DateOfBirth);
list.add(modelItem => item.DateOfWorkStart);
list.add(modelItem => item.NumberOfChildren);
}
}
and finally I would like to use the generated list like this
@foreach (var lambda in list)
{
@Html.DisplayFor(lambda)
}