1

我正在尝试从 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>
4

1 回答 1

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传递总和,但在大多数情况下,我会避开这一点,让自己成为强类型模型。有关使用and的更多信息,请参阅我的答案,以及“ViewBag 和 ViewData 是否也是 asp.net mvc 中状态管理的一部分?” 为什么我通常不会使用它们。如果你确实想使用它们,你可以这样做:ViewBagViewData

public ActionResult About() {
    ViewData["Sums"] = Sum;
    return View(new Voucher().List());
}
<label class ="lbls">@Html.Raw(ViewData["Sums"].Sum)</label>
于 2013-06-05T08:04:21.860 回答