我正在尝试从 db 获取 SUM 并需要将其与列表一起显示。我在控制器中获得了 Sum 的值,但不知道如何将其传递给具有不同模型的视图
这是我的控制器
public class HomeController : Controller
{
public ActionResult About()
{
return View(new Voucher().List());
}
public ActionResult Sum() {
Total Sum = new Voucher().SUM();
ViewData["Sums"] = Sum;
}
}
这里有两个模型
namespace Finance.Models
{
public class AccountDetails
{
public DateTime Updated { get; set; }
public DateTime Date { get; set; }
public string Des { get; set; }
public Decimal Amt { get; set; }
public string VId { get; set; }
public string AId { get; set; }
public string LAId { get; set; }
public string UId { get; set; }
public string CId { get; set; }
public int TXId { get; set; }
public string Per { get; set; }
//public Decimal Sum { get; set; }
}
public class Total
{
public Decimal Sum { get; set; }
}
}
和主要型号
namespace Finance.Models
{
public class Voucher
{
public List<AccountDetails> List()
{
List<AccountDetails> ledger = new List<AccountDetails>();
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = CONNECTION_STRING;
//using (SqlCommand cmd = new SqlCommand("SELECT * FROM Ledger UNION SELECT 0 AS TXID, 0 AS VID, '' AS AID, SUM(AMT) AS SM, '' AS DES, GETDATE() AS DATE, NULL AS LAID, 0 AS UID, 0 AS CID, '' AS PER FROM Ledger ", con))
using (SqlCommand cmd =new SqlCommand("SELECT * FROM Ledger",con))
{
try
{
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read() == true)
{
AccountDetails aclist = new AccountDetails();
aclist.Date = Convert.ToDateTime(rdr["DATE"]);
aclist.AId = (string)rdr["AID"];
aclist.Des = (string)rdr["DES"];
aclist.Amt = (Decimal)rdr["AMT"];
aclist.TXId = (int)rdr["TXID"];
aclist.CId = (string)rdr["CID"];
if (rdr["LAID"] != DBNull.Value)
{
aclist.LAId = (string)rdr["LAID"];
}
else { aclist.LAId = string.Empty; }
if (rdr["UID"] != DBNull.Value)
{
aclist.UId = (string)rdr["UID"];
}
else { aclist.UId = string.Empty; }
if (rdr["VID"] != DBNull.Value)
{
aclist.VId = (string)rdr["VID"];
}
else { aclist.VId = string.Empty; }
if (rdr["PER"] != DBNull.Value)
{
aclist.Per = (string)rdr["PER"];
}
else { aclist.Per = string.Empty; }
ledger.Add(aclist); }
}
catch (Exception e) { throw e; }
finally { if (con.State == System.Data.ConnectionState.Open) con.Close(); }
return ledger;
}
}
}
public Total SUM()
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = CONNECTION_STRING;
Total Tsum = new Total();
using (SqlCommand cmd = new SqlCommand("SELECT SUM(AMT)AS Sum FROM Ledger", con))
{
try
{
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read() == true)
{
if (rdr["Sum"] != DBNull.Value)
{
Tsum.Sum = (Decimal)rdr["Sum"];
}
else { Tsum.Sum = Decimal.Zero; }
//Sum.Add(Tsum);
}
}
catch (Exception e) { throw e; }
finally { if (con.State == System.Data.ConnectionState.Open) con.Close(); }
return Tsum ;
}
}
}
}
}
和视图
@model IEnumerable<Finance.Models.AccountDetails>
<link href="~/Content/ledger.css" rel="stylesheet" />
<script src="~/Content/new.js"></script>
<hgroup class="title"> ...</hgroup>
<section class ="tops">
<div class ="banners">... </div>
<div id ="tables">... </div>
<div id="base" class=" left">
<label class ="lbls right"><i>Balance</i></label>
<label class ="lbls">@ViewData["Sums"]</label>
</div>
<div class ="lowpanels">..</div>
</section>