title: Asp MVC binding calculated results from jQuery to model
body: <p>I am using ASP.NET MVC 3.</p>
  
  <p>I have textboxes and I have a control that displays the calculated value of the sum of these textboxes.  I use jQuery to calculate these results.</p>
  
  <p>I am struggling with post back, then the total is cleared and I don't know how it can retain the calculated results.  So I thought that if I have a property in my view model then it will retain the value on post back.  I tried using Html.TextboxFor for the total and this seems to hold the value when I click the submit button.  But I don't want it to be a textbox, just text, but I still need it to be bound to the view model.</p>
  
  <p>Part of my view model:</p>
  
  <pre><code>public class EditGrantApplicationViewModel
  {
     public decimal? GrossMonthlySalary { get; set; }
     public decimal? SpouseGrossMonthlySalary { get; set; }
     public decimal? AdditionalIncome { get; set; }
     public decimal? ChildSupportIncome { get; set; }
     public decimal? TotalMonthlyIncome
     {
        get { return totalMonthlyIncome; }
        set
        {
           totalMonthlyIncome = GrossMonthlySalary +
              SpouseGrossMonthlySalary +
              AdditionalIncome +
              ChildSupportIncome;
        }
     }
  }
  </code></pre>
  
  <p>Part of my HTML:</p>
  
  <pre><code>&lt;td&gt;&lt;label&gt;Gross Monthly Salary:&lt;/label&gt; &lt;span class="red"&gt;**&lt;/span&gt;&lt;/td&gt;
  &lt;td&gt;@Html.TextBoxFor(x =&gt; x.GrossMonthlySalary, new { @class = "income", maxlength = "10", size = "20" })
     @Html.ValidationMessageFor(x =&gt; x.GrossMonthlySalary)
  &lt;/td&gt;
  
  &lt;td&gt;&lt;label&gt;Spouse Gross Monthly Salary:&lt;/label&gt;&lt;/td&gt;
  &lt;td&gt;@Html.TextBoxFor(x =&gt; x.SpouseGrossMonthlySalary, new { @class = "income", maxlength = "10", size = "20" })
     @Html.ValidationMessageFor(x =&gt; x.SpouseGrossMonthlySalary)
  &lt;/td&gt;
  
  &lt;td&gt;&lt;label&gt;Any Additional Income:&lt;/label&gt;&lt;/td&gt;
  &lt;td&gt;@Html.TextBoxFor(x =&gt; x.AdditionalIncome, new { @class = "income", maxlength = "10", size = "20" })
     @Html.ValidationMessageFor(x =&gt; x.AdditionalIncome)
  &lt;/td&gt;
  
  &lt;td&gt;&lt;label&gt;Child Support Received:&lt;/label&gt;&lt;/td&gt;
  &lt;td&gt;@Html.TextBoxFor(x =&gt; x.ChildSupportIncome, new { @class = "income", maxlength = "10", size = "20" })
     @Html.ValidationMessageFor(x =&gt; x.ChildSupportIncome)
  &lt;/td&gt;
  
  &lt;td&gt;&lt;label class="total"&gt;Total Monthly Income:&lt;/label&gt;&lt;/td&gt;
  &lt;td&gt;
     &lt;label id="TotMonthlyIncome" class="total-amount"&gt;@Html.DisplayTextFor(x =&gt; x.TotalMonthlyIncome)&lt;/label&gt;
     @Html.HiddenFor(x =&gt; x.TotalMonthlyIncome)
  &lt;/td&gt;
  </code></pre>
  
  <p>jQuery for the additions:</p>
  
  <pre><code>$('.income').keyup(function () {
  
     var incomes = $('.income'),
        totDisplay = $('#TotMonthlyIncome'),
        totalDisplay = $('#TotalMonthlyIncome'),
        totalVal = 0;
  
     incomes.each(function () {
        var matches = null;
        // find the number to add to total
        matches = $(this).val().match(/\d+/);
        // not bothering with the regex on totalVal because we set it
        totalVal = (matches !== null ? parseInt(matches[0], 10) : 0) + parseInt(totalVal, 10);
     });
     totalVal = totalVal === 0 ? '' : totalVal;
     totDisplay.text(totalVal);
     $('#TotalMonthlyIncome').val(totalVal);
  });
  </code></pre>
  
  <p>When I type in a value in a textbox then it calculates correctly.  If I type in values in the 4 textboxes then it is calculated correctly.  If I enter a value into on 1 of the textboxes then the TotalMonthlyIncome is null, but when all 4 textboxes have values in it then it has the value of the additions of the textboxes.  Why is it doing this?  Is it something that is not right in my code?</p>