使用ViewBag从控制器传递列表到视图

时间:2020-06-28 07:31:33

标签: c# html sql model-view-controller razor

从控制器中,我正在使用ViewBag将一个AccountNumbers列表传递给View,并且运行良好

控制器代码:

  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();

查看此代码是:

@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>

} 我收到以下错误: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:'无法将类型'int'隐式转换为'System.Collections.IEnumerable'。我想使用ViewBag在视图中显示帐号,表中的总计金额。

1 个答案:

答案 0 :(得分:0)

因为您要传递计数,该计数是int,并且在视图中将其视为IEnunerable。通过而不应用Count()

相关问题