计算数据库表字段 单价总额
(作者:邱名涛,撰写时间:2019年1月16日)
开发工具与关键技术: Visual Studio +SQL Server+C#MVC
控制层代码:
1、封装一个字段,通过ReturnJson返回到页面
public ActionResult SelectMarketingSubdivide()
{
ReturnJson returnJson = new ReturnJson();//实例化类,可以实现传输多条数据。
var list = (from tbMarketingSubdivide in myModels.Yw_MarketingSubdivide
join tbreckoning in myModels.Yw_reckoning on tbMarketingSubdivide.MarketingSubdivideID equals tbreckoning.MarketingSubdivideID
join tbmarketing in myModels.YW_marketing on tbMarketingSubdivide.marketingID equals tbmarketing.marketingID
orderby tbmarketing.marketingID descending//降序
select new QianXiangVo
{
reckoningID = tbreckoning.reckoningID,//结账ID
GatheringMoney = tbreckoning.GatheringMoney,//收款金额
marketingID = tbmarketing.marketingID,//销售ID
MarketingPrice = tbMarketingSubdivide.MarketingPrice,//销售金额
MarketingCode = tbmarketing.MarketingCode.Trim(),//销售单号
}).ToList();
decimal s = 0;
for (int i = 0; i < list.Count; i++)//Count控制层/length页面层
{
s = s + list[i].GatheringMoney;
}
//声明一个数字类型s,然后用for循环去循环,查出Yw_MarketingSubdivide这个表关于价钱的数据,
然后用每次循环的价格都相加,然后计算出总额;
returnJson.GatheringMoney = s;
returnJson.MarketingCode = list[0].MarketingCode;//表通过降序后的第一条数据。
returnJson.MarketingPrice = list[0].MarketingPrice;
return Json(returnJson, JsonRequestBehavior.AllowGet);
}
2、实例化ReturnJson
namespace FZXM.Vo
{
public class ReturnJson
{
/// <summary>
/// 状态
/// </summary>
public decimal GatheringMoney { get; set; }
/// <summary>
/// 文本
/// </summary>
public string MarketingCode { get; set; }
/// <summary>
/// 返回的数据
/// </summary>
public decimal MarketingPrice { get; set; }
public string residue { get; set; }
}
}
页面代码:
封装字段ReturnJson进行传输数据
$(function () {
$.post("SelectMarketingSubdivide", function (data) {//控制层传过来的参数
$("#GatheringMoney").val("¥" + Number(data.GatheringMoney).toFixed(2)); //保留两位小数
$("#GatheringMoney2").val("¥" + Number(data.MarketingPrice).toFixed(2));
$("#aaa").val(data.GatheringMoney);//赋值
})
})
//直接用post去请求控制层的方法,然后显示出他的数据。
(toFixed(2)数字后面保留几位小数)
效果图例: