c# - How to get variable into another razor expression -
string[] showproperties = {"id", "name"}; @* user has many properties, id, name, address, etc... *@ <!-- want show properties in shownproperties list --> @for (int j = 0; j < showproperties.length; j++) { <td> @user.showproperties[j] </td> }
normally @user.id
or @user.name display, in case, [property] dynamically pulled value of array of showcolumns.
the @user.showproperties[j] above won't work, razor won't recognize id property example. there way inject 'id' @user.[] @user.id?
how do @user.(mydynamicproperty)
properly?
if dynamic object of type expandoobject
, simple casting dynamic object type idictionary<string, object>
because expandoobject
's implement interface. here example razor program:
@{ string[] showcolumns = new string[3] {"property1", "property2", "property3"}; dynamic myobject = new system.dynamic.expandoobject(); myobject.property1 = "first property!"; myobject.property2 = "second property!"; myobject.property3 = "third property!"; (int = 0; < showcolumns.length; i++) { var propertyvalues = (idictionary<string, object>)myobject; <p>@propertyvalues[showcolumns[i]]</p> } }
if object not expandoobject
, might little more difficult. difficult, not impossible. @ answer here.
Comments
Post a Comment