两款车型在一个视图(MVC 4)。没有得到使用VALU计算机[] [英] TWO Models in one View (MVC 4). not getting valu using viewdata[]
问题描述
我想从分贝SUM和需要与列表一起显示。我总在控制器的价值,但不知道如何将它传递给具有不同的模型视图
这是我的控制器
公共类HomeController的:控制器
{
公众的ActionResult关于()
{ 返回查看(新券()名单());
}
公众的ActionResult总和(){
合计金额=新券()SUM()。
计算机[概括] = SUM;
}
}
和来这里的两个型号
命名空间Finance.Models
{ 公共类AccountDetails
{
公开日期时间更新{搞定;组; }
公众的DateTime日期{搞定;组; }
公共字符串德{搞定;组; }
公共小数金额{搞定;组; }
公共字符串VID {搞定;组; }
公共字符串援助{搞定;组; }
公共字符串奠定{搞定;组; }
公共字符串UID {搞定;组; }
公共字符串ID {搞定;组; }
公众诠释TxID添加{搞定;组; }
公共字符串每{搞定;组; }
//公共十进制总和{搞定;组; } }
公共类共
{
公共十进制总和{搞定;组; } }}
和主模型
命名空间Finance.Models
{
公共类券
{公开名单< AccountDetails>名单()
{
清单< AccountDetails>总帐=新的List< AccountDetails>();
使用(SqlConnection的CON =新的SqlConnection())
{
con.ConnectionString = CONNECTION_STRING;
使用(CMD的SqlCommand =新的SqlCommand //(SELECT * FROM莱杰UNION SELECT 0 AS TxID添加,0 AS VID,AS AID,SUM(AMT)AS SM,AS DES,GETDATE()日期,NULL作为铺设,0作为UID,0作为CID,AS PER FROM台账,CON))
使用(CMD的SqlCommand =新的SqlCommand(SELECT * FROM台账,CON))
{
尝试
{
con.Open();
SqlDataReader的RDR = cmd.ExecuteReader(); 而(rdr.Read()==真)
{ AccountDetails aclist =新AccountDetails();
aclist.Date = Convert.ToDateTime(RDR [DATE]);
aclist.AId =(字符串)RDR [AID];
aclist.Des =(字符串)RDR [DES];
aclist.Amt =(十进制)RDR [AMT];
aclist.TXId =(INT)RDR [TxID添加];
aclist.CId =(字符串)RDR [CID]; 如果(RDR [LAID]!= DBNull.Value)
{
aclist.LAId =(字符串)RDR [LAID];
}
其他{aclist.LAId =的String.Empty; } 如果(RDR [UID]!= DBNull.Value)
{
aclist.UId =(字符串)RDR [UID];
}
其他{aclist.UId =的String.Empty; }
如果(RDR [VID]!= DBNull.Value)
{
aclist.VId =(字符串)RDR [VID];
}
其他{aclist.VId =的String.Empty; } 如果(RDR [PER]!= DBNull.Value)
{
aclist.Per =(字符串)RDR [PER];
}
其他{aclist.Per =的String.Empty; } ledger.Add(aclist); } }
赶上(例外五){抛出Ë; }
最后{如果(con.State == System.Data.ConnectionState.Open)con.Close(); }
返回台账;
}
}
}
公共总和()
{ 使用(SqlConnection的CON =新的SqlConnection())
{
con.ConnectionString = CONNECTION_STRING;
总TSUM =新总();
使用(CMD的SqlCommand =新的SqlCommand(SELECT SUM(AMT)为SUM FROM台账,CON))
{ 尝试
{
con.Open();
SqlDataReader的RDR = cmd.ExecuteReader();
而(rdr.Read()==真)
{ 如果(RDR [心]!= DBNull.Value)
{
Tsum.Sum =(十进制)RDR [心];
}
其他{Tsum.Sum = Decimal.Zero; }
//Sum.Add(Tsum);
}
}
赶上(例外五){抛出Ë; }
最后{如果(con.State == System.Data.ConnectionState.Open)con.Close(); }
返回TSUM;
}
}
}
}
}
和视图
@model IEnumerable的< Finance.Models.AccountDetails> <链接HREF =〜/内容/ ledger.css的rel =stylesheet属性/>
<脚本SRC =〜/内容/ new.js>< / SCRIPT>
< hgroup类=标题> ...< / hgroup><节类=顶部>
< DIV CLASS =横幅> ...< / DIV>
< DIV ID =表> ...< / DIV>
< DIV ID =基地类=左>
<标签类=lbls权>< I>余量< / I>< /标签>
<标签类=lbls> @ViewData [求和]< /标签> < / DIV>
< DIV CLASS =lowpanels> ..< / DIV>
< /节>
一个单一的的ActionResult
对应一个单一视图,你需要的东西更像是:
公共类AboutViewModel {
公共总MyTotal {搞定;组; }
公共IEnumerable的< AccountDetails> MYLIST {搞定;组; }
}公众的ActionResult关于()
{
VAR券=新券();
VAR模型=新AboutViewModel {
MyTotal = voucher.SUM();
MYLIST = voucher.List();
}
返回查看(模型);
}
和用于查看/主页/ About.cshtml:
@model Finance.Models.AboutViewModel
<链接HREF =〜/内容/ ledger.css的rel =stylesheet属性/>
<脚本SRC =〜/内容/ new.js>< / SCRIPT>
< hgroup类=标题> ...< / hgroup><节类=顶部>
< DIV CLASS =横幅> ...< / DIV>
< DIV ID =表> ...< / DIV>
< DIV ID =基地类=左>
<标签类=lbls权>< I>余量< / I>< /标签>
< - !-------这里更改------------------------------------ ------------ - >
<标签类=lbls> @ Html.Raw(Model.MyTotal.Sum)LT; /标签>
< / DIV>
@foreach(在Model.MyList VAR项){
...渲染的东西...
}
< DIV CLASS =lowpanels> ..< / DIV>
< /节>
您也可以通过新券()。名单()
作为视图模型,并使用 ViewBag
通过总和,但我会从这个引导离开,让自己在大多数情况下一个强类型的模型。见我的答案<一个href=\"http://stackoverflow.com/questions/16482258/storing-a-js-value-from-1-actionresult-to-use-in-another-actionresult/16485025#16485025\">here更多信息的关于使用 ViewBag
和的ViewData code>,同时也为的\"Is ViewBag和ViewData的也是在asp.net mvc的状态管理的一部分吗?为什么我通常不会使用它们。如果你想使用它们,你可以这样做:
公众的ActionResult关于(){
计算机[概括] = SUM;
返回查看(新券()名单());
}
&LT;标签类=lbls&GT; @ Html.Raw(计算机[概括]总和)LT ; /标签&gt;
I am trying to get a SUM from db and need to display it along with the list. I got the value of Sum in controller but don't know how to pass it to the view having a different model
This is my Controller
public class HomeController : Controller
{
public ActionResult About()
{
return View(new Voucher().List());
}
public ActionResult Sum() {
Total Sum = new Voucher().SUM();
ViewData["Sums"] = Sum;
}
}
and here comes the two models
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; }
}
}
and the main model
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 ;
}
}
}
}
}
and the view
@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>
A single ActionResult
corresponds to a single view, you need something more like:
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);
}
And for 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>
You could also pass the new Voucher().List()
as the view model, and use ViewBag
to pass the sum, but I would steer away from this and make yourself a strongly typed model in most cases. See my answer here for more info about using ViewBag
and ViewData
, and also to "Is ViewBag and ViewData also part of state management in asp.net mvc?" for why I wouldn't usually use them. If you do want to use them, you could do:
public ActionResult About() {
ViewData["Sums"] = Sum;
return View(new Voucher().List());
}
<label class ="lbls">@Html.Raw(ViewData["Sums"].Sum)</label>
这篇关于两款车型在一个视图(MVC 4)。没有得到使用VALU计算机[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!