简体   繁体   English

将列表从 Controller 传递到使用 ViewBag 进行查看

[英]Pass List From Controller to View Using ViewBag

From Controller I am passing a list of AccountNumbers using ViewBag to the View, and this is running well从 Controller 我使用 ViewBag 将 AccountNumbers 列表传递给视图,并且运行良好

Controller Code: Controller 代码:

  List<AccountNumberSum> my_output_list = new List<AccountNumberSum>();
      my_output_list.Add(new AccountNumberSum { 
                                                 account_number = oo.CreditAccountNumber,
                                                  total_amount = item1.Amount.Value
                                            });
      var grouped_data = my_output_list.GroupBy(x => x.account_number)
                    .Select(x => new { account_number = x.Key, total_amount = x.Sum(y=> y.total_amount) })
                    .ToList();


           ViewBag.grouped_data = grouped_data.Count();

And View Code For this is:并且查看代码是:

@if (ViewBag.grouped_data != null)
{

    <div class="tb">
        <table id="table_id" class="display" style="width:100%">
            <thead>
                <tr>

                    <th>
                        Sl.No
                    </th>
                    <th>
                        Account Number
                    </th>
                    <th>
                        Total Amount
                    </th>
                </tr>
            </thead>
            @foreach (var item in ViewBag.grouped_data.GetType().GetProperties())


            {

                <tr>
                    <td>
                        @item.account_number



                    </td>
                    <td>
                        @item.total_amount

                    </td>


                </tr>
            }

        </table>
    </div>

} I am getting the following error: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot implicitly convert type 'int' to 'System.Collections.IEnumerable''.我收到以下错误:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:'无法将类型'int'隐式转换为'System.Collections.IEnumerable''。 I want to show account numbers,Total_amount in Table in view using ViewBag.我想使用 ViewBag 在表中显示帐号,Total_amount。

Because you are passing the count, which is int and that you are treating it as IEnunerable in the view.因为您正在传递计数,它是 int 并且您在视图中将其视为IEnunerable Pass it without applying Count() .通过它而不应用Count()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM