我有一个这样的课程
public class AmeInvoice
{
public DateTime invoiceDate { get; set; }
public string invoiceNumber { get; set; }
public string accountNumber { get; set; }
public double amount { get; set; }
public double amountDue { get; set; }
}
我有一个像这样的视图模型
public class AmeInvoiceViewModel
{
public List<AmeInvoice> ppInvoices { get; set; }
public double otherAmount { get; set; }
public double totalDue { get; set; }
}
我的控制器中的动作结果方法是这样的
public actionresult index ()
{
....
....
....
List<AmeInvoice> prideInvoices = new List<AmeInvoice>();
while (reader.Read())
{
prideInvoices.Add(new AmeInvoice()
{
invoiceDate = Convert.ToDateTime(reader["invoicedate"]),
invoiceNumber = reader["invoicenumber"].ToString(),
accountNumber = reader["account"].ToString(),
amount = Convert.ToDouble(reader["amount"]),
amountDue = Convert.ToDouble(reader["amountdue"])
});
}
var myviewModel = new AmeInvoiceViewModel();
myviewModel.ppInvoices = prideInvoices;
myviewModel.otherAmount = 20.20;
myviewModel.totalDue = 30.20;
return View(myviewModel);
}
在我看来,我有这个
@model List<InSCmm.Web.Model.AmeInvoiceViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table>
@foreach (InSCmm.Web.Model.AmeInvoiceViewModel objUser in Model)
{
<tr>
<td>@objUser.</td>
<td>@objUser.</td>
<td>@objUser.</td>
<td>@objUser.</td>
<td>@objUser.</td>
</tr>
}
</table>
我的问题:我对Mvc很新,我希望能够展示 invoiceDate,invoiceNumber,accountNumber,金额,应付在我视图中的表中的金额。 目前,如果我做一个objUser。 (我没有得到这些字段) 请协助。这是正确的道路吗?
答案 0 :(得分:2)
您的观点是单个实体而不是IEnumerable<AmeInvoiceViewModel>
,因此迭代Model
(单个AmeInvoiceViewModel
)将无效。
我认为您正在寻找Model.ppInvoices
,但是您需要迭代AmeInvoice
而不是AmeInvoiceViewModel
。类似的东西:
<table>
@foreach (AmeInvoice invoice in Model.ppInvoices)
{
<tr>
<td>@html.DisplayFor(x => invoice.invoiceDate)</td>
<td>@Html.DisplayFor(x => invoice.invoiceNumber)</td>
<td>@Html.DisplayFor(x => invoice.accountNumber)</td>
@* ... *@
</tr>
}
</table>
答案 1 :(得分:2)
您的视图必须强烈输入您要传递的视图模型,而不是列表:
@model InSCmm.Web.Model.AmeInvoiceViewModel
然后:
<table>
@foreach (var objUser in Model.ppInvoices)
{
<tr>
<td>@objUser.invoiceDate</td>
<td>@objUser.invoiceNumber</td>
<td>@objUser.accountNumber</td>
<td>@objUser.amount</td>
<td>@objUser.amountDue</td>
</tr>
}
</table>
<div>
Total due: @Html.DisplayFor(x => x.totalDue)
</div>