1

From Controller I am passing a list of AccountNumbers using ViewBag to the View, and this is running well

Controller Code:

  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''. I want to show account numbers,Total_amount in Table in view using ViewBag.

1 Answers1

0

Because you are passing the count, which is int and that you are treating it as IEnunerable in the view. Pass it without applying Count().

sayah imad
  • 1,507
  • 3
  • 16
  • 24
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197