0

In a razor-view, how can I check the existence of a property in a model?

if (Model._myProp != null)  <--- error .RuntimeBinder.RuntimeBinderException if Model does not contain _myProp
    {
      ...do something
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
reinhard
  • 818
  • 1
  • 10
  • 24
  • Possible duplicate of http://stackoverflow.com/questions/3468250/c-sharp-elegant-way-to-check-if-a-propertys-property-is-null – Aditya Singh May 20 '13 at 06:41
  • Please, stop referring to "ASP.NET MVC" simply as "MVC". One is a framework, while other is a language-independent design pattern. It's like calling IE - "the internet" – tereško May 20 '13 at 08:00

1 Answers1

1

You do not want to check that one of the model's properties has the value null you want to check the type for it is having a property or not and ofcourse you solution will not work that way but @Satpal's wont work too.

Use stronly typed views and separate different properties into interfaces and use them with partial views.

Peter Kiss
  • 9,309
  • 2
  • 23
  • 38