我正在尝试从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>
答案 0 :(得分:2)
单个ActionResult
对应于单个视图,您需要更多类似的内容:
public class AboutViewModel {
public Total MyTotal { get; set; }
public IEnumerable<AccountDetails> MyList { get; set; }
}
public ActionResult About()
{
var voucher = new Voucher();
var model = new AboutViewModel {
MyTotal = voucher.SUM();
MyList = voucher.List();
}
return View(model);
}
对于Views / Home / About.cshtml:
@model Finance.Models.AboutViewModel
<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>
<!-- ------- Change here ------------------------------------------------ -->
<label class ="lbls">@Html.Raw(Model.MyTotal.Sum)</label>
</div>
@foreach (var item in Model.MyList) {
... render stuff ...
}
<div class ="lowpanels">..</div>
</section>
您也可以将new Voucher().List()
作为视图模型传递,并使用ViewBag
来传递总和,但我会避开这种情况,并在大多数情况下使自己成为强类型模型。请参阅我的回答here for more info,了解如何使用ViewBag
和ViewData
,以及"Is ViewBag and ViewData also part of state management in asp.net mvc?",了解我通常不会使用它们的原因。如果你想使用它们,你可以这样做:
public ActionResult About() {
ViewData["Sums"] = Sum;
return View(new Voucher().List());
}
<label class ="lbls">@Html.Raw(ViewData["Sums"].Sum)</label>