如何显示从控制器传递的视图中的列表项?

时间:2013-05-03 10:29:09

标签: asp.net-mvc razor

我正在开发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>

1 个答案:

答案 0 :(得分:1)

将部分视图添加到项目中,将其模型设置为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>