How can I create a form for dynamic properties?
So I understand this is how you dynamically get a property of an object:
ObjectName.GetType().GetProperty("nameofproperty")
How can I include the above in the form? I have tried doing this:
//loop through objects in model
@Html.CheckBoxFor(model => m.ElementAt(i).Person.GetType().GetProperty("isAvailable"), new { @class = "person-avail" })
and within the model I have Person defined as a property like this:
public Person Person { get; set; }
and the Person class has it's own properties (the ones I want to create a form for):
public class Person
{
public String name {get; set;}
public bool isAvailable {get; set;}
.
.
. etc
}
So at the moment this:
@Html.CheckBoxFor(model => m.ElementAt(i).Person.GetType().GetProperty("isAvailable"), new { @class = "person-avail" })
Gives me an exception saying:
Cannot implicitly convert type 'System.Reflection.PropertyInfo' to 'bool'
I understand what the problem is but how would I go about working around it?