我正在试图全面了解我的看法。 我很困惑,我该怎么办?请帮帮我.. 我不明白我应该在哪里使用查询和ofc我需要在我的视图中显示但是如何?
namespace BOL1
{
public class ADetailsVm
{
public List<BOL1.tbl_Transiction> Payables { get; set; }
public List<BOL1.tbl_Transiction> Reciveables { get; set; }
}
}
的DbContext
public partial class bankingEntities : DbContext
{
public bankingEntities()
: base("name=bankingEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<tbl_Accounts> tbl_Accounts { get; set; }
public virtual DbSet<tbl_Transiction> tbl_Transiction { get; set; }
public virtual DbSet<tbl_TransictionType> tbl_TransictionType { get; set; }
public DbSet<ADetailsVm> ADetailsVm { get; set; }
}
}
public class Details : Controller
{
private TransictionBs objbs;
public Details()
{
objbs = new TransictionBs();
}
// GET: Shinwari/AccountDetails
[HttpGet]
public ActionResult Index(int accountid)
{
ADetailsVm v = new ADetailsVm();
//Load both the collection properties
v.Payables = objbs.GetALL().Where(p => p.AId == accountid && p.tbl_TransictionType.Type.Contains("Payable")).ToList();
v.Reciveables = objbs.GetALL().Where(r => r.AId==accountid && r.tbl_TransictionType.Type.Contains("Reciveable")).ToList();
return View(v);
@model BOL1.ADetailsVm
@{
ViewBag.Title = "Index";
}
<h2>AccountDetails</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table id="Payables" class="table">
<tr>
<th>
Date
</th>
<th>
Discription
</th>
<th>
Amount
</th>
</tr>
@foreach (var item in Model.Payables)
{
<tr>
<td>
@item.Date
</td>
<td>
@item.TDiscription
</td>
<td>
@item.Amount
</td>
</tr>
}
</table>
答案 0 :(得分:2)
在视图模型的Sum
集合属性的Amount
属性(假设它是数字类型)上使用LINQ Payables
扩展方法。
<h2> @Model.Payables.Sum(s=>s.Amount) </h2>
或者,如果您不喜欢在剃刀视图中添加如此多的C#代码(和我一样:)),您可以添加一个新属性来存储视图模型中的总数。
public class ADetailsVm
{
public decimal TotalPayableAmount { set;get;}
public List<BOL1.tbl_Transiction> Payables { get; set; }
public List<BOL1.tbl_Transiction> Reciveables { get; set; }
}
在您的操作方法中,调用Sum
方法并将值设置为新的TotalPayableAmount
属性。
public ActionResult Index(int accountid)
{
var v = new ADetailsVm();
v.Payables = objbs.GetALL().Where(p => p.AId == accountid &&
p.tbl_TransictionType.Type.Contains("Payable")).ToList();
v.Reciveables = objbs.GetALL().Where(r => r.AId == accountid &&
r.tbl_TransictionType.Type.Contains("Reciveable")).ToList();
v.TotalPayableAmount= v.Payables.Sum(s=>s.Amount)
return View(v);
}
并在您的视图中
@model ADetailsVm
<h2>@Model.TotalPayableAmount</h2>