0

我正在开发 MVC 应用程序。

我想在控制器中创建列表并将其传递给视图。

我已经在控制器中编写了该方法,但不知道如何在表单视图中调用它并显示它返回的值。

控制器中的方法。

public List<Invoice> GetInvoiceList(int Pid)
        {
            List<Invoice> Invoices = new List<Invoice>();          
            var InvoiceList = (from i in db.Invoices
                          where i.PaymentAdviceId == Pid 
                          select i);
            Invoices = InvoiceList.ToList();

            return (Invoices);
        }

查看代码

  <div class="row-fluid">
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>Advice No
                  </th>
                    <th>
                     Invoices
                    </th>
               </tr>
            </thead>

             @foreach (var item in Model)
            {
                <tbody>
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.AdviceNo)
                        </td>

                       I wan to call the controller method GetInvoiceList here and
                       want to display list items here... 
                        <td>


                    </tr>
                </tbody>
4

1 回答 1

1

将 PartialView 添加到您的项目中,并将其模型设置为List<Invoice>

然后修改您的代码:

    public PartialViewResult GetInvoiceList(int Pid)
    {
        List<Invoice> Invoices = new List<Invoice>();          
        var InvoiceList = (from i in db.Invoices
                      where i.PaymentAdviceId == Pid 
                      select i);
        Invoices = InvoiceList.ToList();

        return PartialView("partialViewName", Invoices);
    }

在你的另一种观点中:

    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.AdviceNo)
        </td>
        <td> @Html.Action("GetInvoiceList", new {Pid = item.id})</td>
    </tr>
于 2013-05-03T10:40:23.783 回答