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))
ormvchtmlstring.create(stringwithmarkup)
- both of require known type or local variable, , don't work binding property of anonymous
object
- lists several failed approaches, settles on creating
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
.
- accepted answer helps explains what's happening:
attempted solutions
okay, assume i'll swap
string
properties out 1 ofihtmlstring
properties... nope. since have anonymous type, razor engine doesn't knowmyfield
ihtmlstring
, , (i assume) calls.tostring()
, encoded usual.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:
set
viewbag.myfield
in parentview
before rendering eachpartial 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.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 ofobject
), i'm assuming it's razor engine inspectingmyfield
object type directly special rendering of known type (ihtmlstring
), rather seeing unknownobject
, needing access property not known exist @ compile time.
Comments
Post a Comment