【问题标题】:Asp MVC binding calculated results from jQuery to modelAsp MVC 绑定计算结果从 jQuery 到模型
【发布时间】:2011-09-08 14:32:38
【问题描述】:

我正在使用 ASP.NET MVC 3。

我有文本框,我有一个控件来显示这些文本框之和的计算值。我使用 jQuery 来计算这些结果。

我正在努力回发,然后总数被清除,我不知道它如何保留计算结果。所以我认为如果我的视图模型中有一个属性,那么它将保留回发时的值。我尝试使用 Html.TextboxFor 作为总数,当我单击提交按钮时,这似乎保留了该值。但我不希望它是文本框,只是文本,但我仍然需要将它绑定到视图模型。

我的视图模型的一部分:

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;
      }
   }
}

我的 HTML 的一部分:

<td><label>Gross Monthly Salary:</label> <span class="red">**</span></td>
<td>@Html.TextBoxFor(x => x.GrossMonthlySalary, new { @class = "income", maxlength = "10", size = "20" })
   @Html.ValidationMessageFor(x => x.GrossMonthlySalary)
</td>

<td><label>Spouse Gross Monthly Salary:</label></td>
<td>@Html.TextBoxFor(x => x.SpouseGrossMonthlySalary, new { @class = "income", maxlength = "10", size = "20" })
   @Html.ValidationMessageFor(x => x.SpouseGrossMonthlySalary)
</td>

<td><label>Any Additional Income:</label></td>
<td>@Html.TextBoxFor(x => x.AdditionalIncome, new { @class = "income", maxlength = "10", size = "20" })
   @Html.ValidationMessageFor(x => x.AdditionalIncome)
</td>

<td><label>Child Support Received:</label></td>
<td>@Html.TextBoxFor(x => x.ChildSupportIncome, new { @class = "income", maxlength = "10", size = "20" })
   @Html.ValidationMessageFor(x => x.ChildSupportIncome)
</td>

<td><label class="total">Total Monthly Income:</label></td>
<td>
   <label id="TotMonthlyIncome" class="total-amount">@Html.DisplayTextFor(x => x.TotalMonthlyIncome)</label>
   @Html.HiddenFor(x => x.TotalMonthlyIncome)
</td>

用于添加的jQuery:

$('.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);
});

当我在文本框中输入一个值时,它会正确计算。如果我在 4 个文本框中输入值,则计算正确。如果我在 1 个文本框中输入一个值,则 TotalMonthlyIncome 为空,但是当所有 4 个文本框都有值时,它具有添加文本框的值。为什么要这样做?我的代码中有什么不正确的地方吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2


    【解决方案1】:

    您可以创建隐藏输入并将其与 viewmodel 属性绑定。

    <%: Html.HiddenFor(m => m.CalculatedValue) %>
    <div id="DisplayCalculatedValue"><%: Model.CalculatedValue %></div>
    
    <script type="text/javascript">
    var value = // calculate it
    $('#CalculatedValue').val(value);
    $('#DisplayCalculatedValue').html(value);
    </script>
    

    【讨论】:

    • 您的代码无法正常工作。我需要将计算结果显示为用户在文本框中键入的内容。我通过 jQuery 做到这一点。您的第二行代码只显示值,我需要即时计算它。
    • @Brendan Vogt,您可以调整您的 jQuery 代码,以便除了更新显示的值之外,它还会更新隐藏的字段值。
    • @Darin 但问题是它在回发时失去了价值。你有一些示例代码给我吗?总数仅用于显示给用户,我不将其保存到数据库中。我只是认为通过将总计绑定到模型将使其保持状态。
    • 就像@Darin Dimitrov 所说,您应该使用计算值更新隐藏字段。这对我来说很明显。我想我应该更具体一些。我更新了我的答案。
    • @Darin:这里发生了什么,我没有让它正常工作。我已经更新了我原来的帖子。
    【解决方案2】:

    TotalMonthlyIncome 属性的设置器中,您必须考虑到您的小数可能为空:

    private decimal? totalMonthlyIncome;
    public decimal? TotalMonthlyIncome
    {
        get { return totalMonthlyIncome; }
        set
        {
            totalMonthlyIncome =
                (GrossMonthlySalary.HasValue ? GrossMonthlySalary.Value : 0m) +
                (SpouseGrossMonthlySalary.HasValue ? SpouseGrossMonthlySalary.Value : 0m) +
                (AdditionalIncome.HasValue ? AdditionalIncome.Value : 0m) +
                (ChildSupportIncome.HasValue ? ChildSupportIncome.Value : 0m);
        }
    }
    

    【讨论】:

    • 谢谢。 HasValue 和 Value 属性是否仅适用于可空类型?
    • @Brendan Vogt,它仅适用于您正在使用的 System.Nullable&lt;T&gt; 类型。
    猜你喜欢
    • 2019-11-11
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2016-06-04
    相关资源
    最近更新 更多