c# - ASP.NET MVC '@model dynamic' not recognizing Model properties when partial view is in Shared folder -


not duplicate of: mvc razor dynamic model, 'object' not contain definition 'propertyname'

according answers there,

according david ebbo, can't pass anonymous type dynamically-typed view because anonymous types compiled internal. since cshtml view compiled separate assembly, can't access anonymous type's properties.

why code below - allegedly should never work - work had expected when partial view located in "/home/_partial.cshtml", stops working when moved "/shared/_partial.cshtml"?

using asp.net 4.5 (and previous versions), following produces text "hello, world!" web browser:

~/controllers/homecontroller.cs

using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc;  namespace testdynamicmodel.controllers {     public class homecontroller : controller     {         public actionresult index()         {             return view();         }     } } 

~/views/home/index.cshtml

@html.partial("_partial", new { text = "hello, world!", shouldrender = true } ) 

~/views/home/_partial.cshtml

@model dynamic @if (!model.shouldrender) {     <p>nothing see here!</p> } else {     <p>@model.text</p> } 

however, when _partial.cshtml instead moved ~/views/shared/_partial.cshtml, following error thrown in _partial.cshtml (line 2):

'object' not contain definition 'shouldrender' 

upon inspecting model in debugger, find following properties:

model { text = hello, world!, shouldrender = true } 

while question pertaining behavior of asp.net mvc, , aware of workarounds, not sure is. here workaround wants code work: dynamic anonymous type in razor causes runtimebinderexception


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -