2

我有这样的课

            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。(我没有得到这些字段)请协助。这是正确的道路吗?

4

2 回答 2

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>
于 2013-08-23T16:36:42.003 回答
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>
于 2013-08-23T16:35:44.757 回答