title: Formatting Values in Razor in ASP.NET MVC
body: <p>I'm working on an ASP.NET MVC app that uses Razor as the view engine. I am trying </p>
  
  <pre><code>&lt;div class="form-group"&gt;
    &lt;label class="control-label" for="price"&gt;Price&lt;/label&gt;
    &lt;div class="input-group"&gt;
      &lt;div class="input-group-addon"&gt;$&lt;/div&gt;
      &lt;input id="price" name="price" class="form-control" type="text" autocomplete="off" value="@Model.Price.ToString("G")" /&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>When this code runs, I get an error that says:</p>
  
  <pre><code>Cannot perform runtime binding on a null reference
  </code></pre>
  
  <p>If I replace <code>@Model.Price.ToString("G")</code> with just <code>@Model.Price</code>, my app works. However, the decimal value is not formatted properly. The Price variable comes from my controller action. In the controller, I have:</p>
  
  <pre><code>public ActionResult View(int? id)
  {
    Product product = null;
    if (id.HasValue)
      product = Product.LoadFromDb(id.Value);
    else
      product = new Product();
  
    // product.Price is a nullable decimal (i.e. decimal?)
  
    return View(product);
  }
  </code></pre>
  
  <p>How do I format the Price in my view to be up to two decimal places?</p>
  
  <p>Thanks!</p>