asp.net mvc 4 - Emitting an HTML string from anonymous type property in a repeated, dynamically-typed Partial View -


i passing anonymous type dynamic partial view part of @model, , 1 of properties string contains html. when use htmlhelper methods render property, razor engine encoding string, resulting in literal text on page - <i>text</i> in case, instead of desired text.

since dynamically typed view, cannot call property directly. specifically, if try bind @model.myfield, runtimebindingexception:

'object' not contain definition 'myfield' 

ideally create type (or @ least interface) specify view (which recommend optimal solution), scope of work not permit this. plus i'm using partial view in first place can recycle template different types, have same property names not same type properties (yay legacy code!).

i have looked @ several related questions address similar issues, answers not work specific situation (due needing anonymous type passed @model dynamic view)).

  • displaying html string model in mvc razor view

    • lists several failed approaches, settles on creating ihtmlstring via @(new htmlstring(stringwithmarkup)) or mvchtmlstring.create(stringwithmarkup)
    • both of require known type or local variable, , don't work binding property of anonymous object
  • asp.net mvc4 - display html containing string raw html

    • accepted answer helps explains what's happening:

      all output helpers , other elements in razor put through httputility.htmlencode, unless implement ihtmlstring.

attempted solutions

  1. okay, assume i'll swap string properties out 1 of ihtmlstring properties... nope. since have anonymous type, razor engine doesn't know myfield ihtmlstring, , (i assume) calls .tostring(), encoded usual.

  2. alright, maybe @html.displayfor smarter? yes, access denied:

    'system.web.mvc.htmlhelper' has no applicable method named 'displayfor' appears have extension method name. extension methods cannot dynamically dispatched. consider casting dynamic arguments or calling extension method without extension method syntax.

    oh, right. dynamically dispatched - can't call extension methods on anonymous methods, because razor doesn't know are. because i'm using @model dynamic explicitly tell it, jedi-style, "you don't need see identification". if knew type was, yes, cast object or call extension method without using syntax - again, dynamic , anonymous. sort of chicken , egg issue here.

i found / compiled 2 solutions, neither of happy with. ymmv:

  1. set viewbag.myfield in parent view before rendering each partial view.

    okay, should have figured 1 out lot sooner, had reminded of possibility here because use (preferring strongly-typed views whenever possible). did try on, due way i'm rendering partial multiple times didn't seem appropriate. still don't because in parent view, have keep updating viewbag.myfield before every call @html.partial (6 times use case). puts c# code and variable reuse way down page in middle of content, easy miss , hard maintain.

  2. use reflection: object myfield = ((type)model.gettype()).getproperty("myfield").getvalue(model);

    this decided go use case. though reflection wasn't intended scenario, though requires little error checking. people maintaining more familiar reflection .net mvc, , consolidates code 1 spot - on page matters to, , @ top rest of "server-side" manipulations. no repeated calls or hunting down references.

    i'm not entirely clear on why works (also works dynamic instead of object), i'm assuming it's razor engine inspecting myfield object type directly special rendering of known type (ihtmlstring), rather seeing unknown object , needing access property not known exist @ compile time.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -