0

I'm trying to pass a dynamic model to a partial view in ASP.NET Core 2.2 (using a partial view to add consistency and maintainability to my code)

Code :

@model dynamic
@{
    var asInput = Model.asInput ?? false;
    var title = Model.title ?? "";
    var dataTooltip = string.IsNullOrWhiteSpace(Model.title) ? "" : "enabled";

    var inputType = "submit";
    if (Model.inputType != null) { inputType = Model.inputType; }

    var cssClasses = "";
    if (string.IsNullOrWhiteSpace(Model.cssClass)) { cssClasses = Model.cssClass; }
}

@if (!asInput)
{
    <h1 class="far fa-eye"></h1>
}
else
{
    <input type="submit" value="&#xf06e;" class="btn btn-block btn- 
   primary @cssClasses" data-tooltip="@dataTooltip" title="@title" />
}

Error :

enter image description here

Error Text (when title is null):

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
  HResult=0x80131500
  Message='<>f__AnonymousType1<bool>' does not contain a definition for 'title'

in the above screenshot, "asInput" and "title" have values, but if they did not they would also produce the same error. As you can see I started with null coalescing but eventually tried a simple "if" which still failed

using "IsNullOrWhiteSpace(Model.Property)" is still an error cause the error is produced when accessing Model.Property

Ninjanoel
  • 2,864
  • 4
  • 33
  • 53
  • 6
    Every time a developer uses `dynamic`, a kitten dies. In other words, don't use it! There's almost certainly several better alternatives. – DavidG Jan 08 '19 at 17:00
  • @DavidG it has it's uses, my alternative here is using full on class, which seams unnecessary. any suggestions? – Ninjanoel Jan 08 '19 at 17:07
  • 1
    Why is another class unnecessary? It clearly solves the problem and avoids the need for dynamic (which by the way is horribly slow) – DavidG Jan 08 '19 at 17:15
  • @DavidG it's unnecessary because i can just use dynamic instead (on the brink of a recursive argument here. lol). it appears you may be correct,I may have to use a class instead, cant see this issue addressed online anywhere. – Ninjanoel Jan 08 '19 at 17:21
  • also... dynamic clearly solves the problem and avoids the need for class – Ninjanoel Jan 08 '19 at 17:22
  • 1
    With that argument, use dynamic across your entire application then you never have to create another class ever again. Right? Using dynamic is almost always a nasty hack. And check [here](https://stackoverflow.com/questions/7478387/how-does-having-a-dynamic-variable-affect-performance) and you will get an insight into just how much slower it is too. – DavidG Jan 08 '19 at 17:26
  • 1
    fine fine, i'm convinced. other option could have been using the ViewBag... which is just another type of dynamic variable i suppose, so I'll just define a class. – Ninjanoel Jan 08 '19 at 17:29
  • When this exception is thrown, the property you try to access is not null or empty, it actually does not exist on the object that is passed from the controller to the view. How do you generate this model object? By Deserialization from Json, for example or some other way? – NineBerry Jan 08 '19 at 18:03
  • @NineBerry i was using a 'partial' tag and passing `new { property = "value" }', with whatever properties and values i may need, was hoping i could intentionally leave some fields empty, but as you say, it's not a null exception, it's a "this property dont even exist on the object" error. using a class now instead. – Ninjanoel Jan 09 '19 at 09:24

0 Answers0