我试图获取数据表中列的总和,其中数据的来源来自与数据表使用的API不同的表。
数据表正在使用“购买”表中的数据,该表与“手机”表相关,其中存在“SalesPoints”数据,您可以从下面的代码段中看到。我想得到SalesPoints列的总和。
<script>
$(document).ready(function () {
var table = $("#purchases").DataTable({
rowReorder: {
selector: 'td:nth-child(2)'
},
ajax: {
url: "/api/purchases",
dataSrc: ""
},
columns: [
{
data: "customer.name"
},
{
data: "handset.name"
},
{
data: "handset.salesPoints"
},
{
data: "datePurchased"
}
],
responsive: true
});
});
为了得到这笔钱,我在控制器中包含了以下代码:
private void ComputeBySalesSalesID(DataSet dataSet)
{
// Presumes a DataTable named "purchases" that has a column named "handset.salesPoints."
DataTable table;
table = dataSet.Tables["purchases"];
// Declare an object variable.
object sumObject;
sumObject = table.Compute("SUM([handset.salesPoints])", "");
ViewBag.TotalValue = sumObject.ToString();
}
要显示此总和,我在视图中包含了“@ Html.Label((String)ViewBag.TotalValue)”;当我运行程序时,这个标签会抛出一个异常,说它不能为空。
我会非常感谢对此的任何见解,因为它阻碍了我很快就会完成一个项目。谢谢!
编辑:查看代码
@model IEnumerable<SmashingTargets.Models.Purchase>
@{
ViewBag.Title = "List";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Sales</h2>
<p>
@Html.ActionLink("My Profile", "Profiles", "Sales", null, new { @class =
"btn btn-primary" })
</p>
@*Display Sum total*@
<div class="form-group">
@Html.Label((String)ViewBag.TotalValue)
</div>
<table id="purchases" class="table table-striped table-bordered table-hover
table-responsive">
<thead>
<tr>
<th>Customer</th>
<th>Handset</th>
<th>Points Earned</th>
<th>Date Purchased</th>
</tr>
</thead>
<tbody>
</tbody>
</table>