title: Is there a proper way to check values of properties of a model in an EditorTemplate
body: <p>I want to design my custom editor templates so that they will work even when they are passed a null model. i.e., <code>@Html.EditorForModel()</code> when <code>Model</code> is null.</p>
  
  <p>The problem I have is that when I am in an EditorTemplate, I sometimes need to access one of the properties of the model, and it gets pretty old writing <code>@if(Model != null &amp;&amp; Model.[Property] ...)</code> </p>
  
  <p>for example</p>
  
  <pre><code>@model MyObject
  @if(Model.BoolProperty) // throws NullReferenceException
  {
      &lt;div&gt;...additional stuff here&lt;/div&gt;
  }
  
  @Html.EditorFor(m =&gt; m.OtherProperty)
  </code></pre>
  
  <p>I considered adding an extension method like the following</p>
  
  <pre><code>public static R GetValue&lt;T, R&gt;(this WebViewPage&lt;T&gt; viewPage, Func&lt;T, R&gt; selector)
  {
      if (selector == null) throw new ArgumentNullException("selector");
      if (viewPage == null) throw new ArgumentNullException("viewPage");
      if (viewPage.Model == null) return default(R);
      return selector(viewPage.Model);
  }
  </code></pre>
  
  <p>and using that in the EditorTemplate like this</p>
  
  <pre><code>@model MyObject
  @if(this.GetValue(m =&gt; m.BoolProperty)) // Safely gets value or false
  {
      &lt;div&gt;...additional stuff here&lt;/div&gt;
  }
  
  @Html.EditorFor(m =&gt; m.OtherProperty)
  </code></pre>
  
  <p>but <strong>I want to know if there is a built in or "proper" way to try to access these properties if the model exists, without throwing a <code>NullReferenceException</code></strong>.</p>