{"action":"created","issue":{"url":"https://api.github.com/repos/Antaris/RazorEngine/issues/264","labels_url":"https://api.github.com/repos/Antaris/RazorEngine/issues/264/labels{/name}","comments_url":"https://api.github.com/repos/Antaris/RazorEngine/issues/264/comments","events_url":"https://api.github.com/repos/Antaris/RazorEngine/issues/264/events","html_url":"https://github.com/Antaris/RazorEngine/issues/264","id":67214018,"number":264,"title":"Ability to test for undefined property on a dynamic model.","user":{"login":"robmen","id":131904,"avatar_url":"https://avatars.githubusercontent.com/u/131904?v=3","gravatar_id":"","url":"https://api.github.com/users/robmen","html_url":"https://github.com/robmen","followers_url":"https://api.github.com/users/robmen/followers","following_url":"https://api.github.com/users/robmen/following{/other_user}","gists_url":"https://api.github.com/users/robmen/gists{/gist_id}","starred_url":"https://api.github.com/users/robmen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/robmen/subscriptions","organizations_url":"https://api.github.com/users/robmen/orgs","repos_url":"https://api.github.com/users/robmen/repos","events_url":"https://api.github.com/users/robmen/events{/privacy}","received_events_url":"https://api.github.com/users/robmen/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-04-08T20:33:23Z","updated_at":"2015-04-09T12:25:39Z","closed_at":null,"body":"I have templates that take *very* dynamic models. For example, I often find myself want to do something like:\r\n\r\n    <div>\r\n    @if (Model.Foo.Link != null) {\r\n      <a href=\"@Model.Foo.Link\">@Model.Foo.Text</a>\r\n    }\r\n    else {\r\n      <p>@Model.Foo.Text</p>\r\n    }\r\n    </div>\r\n\r\nWhere `Foo` is 100% `dynamic` and may or may not have had a `Link` string property assigned to it.\r\n\r\nNow, I have enabled `AllowMissingPropertiesOnDynamic`, like so:\r\n\r\n    var config = new TemplateServiceConfiguration();\r\n    config.AllowMissingPropertiesOnDynamic = true;\r\n    config.Namespaces.Add(\"System.IO\");\r\n    config.Namespaces.Add(\"RazorEngine.Text\");\r\n\r\nAnd simply referencing an absent dynamic property does seem to result to \"empty\". For example: `<a href='@Model.Foo.Link'>` will leave the `href` empty. However, testing for the missing property (like above) will throw an exception (I don't have it in front of me but I can get it if desired).\r\n\r\nI wandered through the RazorEngine code for a while and it seems an `object` is being newed up for missing properties when `AllowMissingPropertiesOnDynamic` is `true`. I had the thought that if that object was static, it could maybe be compared against. Maybe something like any of the following:\r\n\r\n    @if (Model.Foo.Link == Razor.Undefined)\r\n    @if (Model.Foo.Link == Undefined())\r\n    @if (Undefined(Model.Foo.Link))\r\n\r\nAnyway, you'll know better than I what might work. I just hope there is something that could be added. Thanks.\r\n\r\nPS: it's totally possible I've missed a mechanism to accomplish testing for an undefined property in a template. If so, I'd really appreciate a pointer to it and I'll happily go read. :sweat_smile: "},"comment":{"url":"https://api.github.com/repos/Antaris/RazorEngine/issues/comments/91213962","html_url":"https://github.com/Antaris/RazorEngine/issues/264#issuecomment-91213962","issue_url":"https://api.github.com/repos/Antaris/RazorEngine/issues/264","id":91213962,"user":{"login":"matthid","id":1866463,"avatar_url":"https://avatars.githubusercontent.com/u/1866463?v=3","gravatar_id":"","url":"https://api.github.com/users/matthid","html_url":"https://github.com/matthid","followers_url":"https://api.github.com/users/matthid/followers","following_url":"https://api.github.com/users/matthid/following{/other_user}","gists_url":"https://api.github.com/users/matthid/gists{/gist_id}","starred_url":"https://api.github.com/users/matthid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matthid/subscriptions","organizations_url":"https://api.github.com/users/matthid/orgs","repos_url":"https://api.github.com/users/matthid/repos","events_url":"https://api.github.com/users/matthid/events{/privacy}","received_events_url":"https://api.github.com/users/matthid/received_events","type":"User","site_admin":false},"created_at":"2015-04-09T12:25:39Z","updated_at":"2015-04-09T12:25:39Z","body":"There currently is no way to do this as long as `AllowMissingPropertiesOnDynamic` is `true` if you are fine with that you can check against an empty string:\r\n\r\n```csharp\r\n@if (!string.IsNullOrEmpty((string)Model.Foo.Link)) {\r\n```\r\n\r\nif you are not sure if `Link` can be casted to to `string` you can use:\r\n```csharp\r\n@if (!string.IsNullOrEmpty(Model.Foo.Link.ToString())) {\r\n```\r\nWhich should almost always work.\r\n\r\nOn the other hand you can set `AllowMissingPropertiesOnDynamic` back to `false` and do it the same way you would do it in C#:\r\n\r\n```csharp\r\n@try {\r\n  // save all properties which might be missing beforehand in variables (otherwise there will be partial ouput).\r\n  var link = Model.Foo.Link; \r\n  // Use them as you would normally.\r\n  <a href=\"\"@link\"\">@Model.Foo.Text</a>\r\n}\r\ncatch (RuntimeBinderException) {\r\n  <p>@Model.Foo.Text</p>\r\n}\r\n``` \r\n"}}