-1

i want to print the viewbag recieved from the controller but i cant, my code in the controller is here:

var qry = from c in db.Customers
          join o in db.Orders on id equals o.CustomerID
          where id == o.CustomerID
          select new {o.OrderID ,o.OrderDetails};
ViewBag.OrdersForUser = qry.ToList();

the printing code in my view is :

@foreach (var order in ViewBag.OrdersForUser)
{
   @order
}

the printed text right now is:

{ OrderID = 1, OrderDetails = System.Collections.Generic.HashSet`1[FinalProject.Models.OrderDetail] }

the type of OrderID is int, the type of OrderDeatils is ICollection i want to print the data in the hash set (and not the decleration like now) , and to split the Order Id into other space.

  • 1
    `OrdersForUser` is a collection of anonymous objects which are internal and can't be access in the view. Create a view model with the 2 properties for `OrderID` and `OrderDetails` and populate it - `select new yourModel { .... }` and return that to the view. –  Jun 24 '15 at 10:20

1 Answers1

1

ViewBag is a dynamic type. And you assign an anonymous type, then you cant get its type in view side.

controller

var qry = from c in db.Customers
      join o in db.Orders on id equals o.CustomerID
      where id == o.CustomerID
      // in this line, what is the type of list ? You should define its type
      // for example:
      select new SomeType{OrderId = o.OrderID ,OrderDetails = o.OrderDetails}
      //select new {o.OrderID ,o.OrderDetails}; 

ViewBag.OrdersForUser = qry.ToList();

and then in your

view

@foreach (var order in (List<SomeType>)ViewBag.OrdersForUser)
{
   @order
}
  • List that you return from controller, should not be anonymous type. (select new SomeType)
  • In view, you should define viewbag type. (List)ViewBag.OrdersForUser)

AFTER COMMENT

Or if there is relation definitions between your entities, you can get only order details like following :

controller:

ViewBag.OrdersForUser = db.OrderDetails.Where(d=>d.Order.CustomerId == id);

view :

@foreach (var orderDetail in (List<OrderDetail>)ViewBag.OrdersForUser)
{
   @orderDetail.Order.xxx
}
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
  • An exception of type 'System.InvalidCastException' occurred in System.Core.dll but was not handled in user code Additional information: Unable to cast object of type 'System.Collections.Generic.List`1[<>f__AnonymousType5`1[System.Collections.Generic.ICollection`1[FinalProject.Models.OrderDetail]]]' – Hen Porcilan Jun 24 '15 at 10:29
  • the o.OrderDetails var is ICollection, i want it to be printing and the error is becuase i cannot cast it, i tried both: foreach (var order in (List)ViewBag.OrdersForUser) { order } ... and also cast to OrderDetail list – Hen Porcilan Jun 24 '15 at 10:34
  • You should define a class something like this class CustomerOrder{int OrderId, List OrderDetails} – AliRıza Adıyahşi Jun 24 '15 at 10:38
  • i created the class, i got the same error:my code is: foreach (var order in (List)ViewBag.OrdersForUser) .... and the error is Unable to cast object of type 'System.Collections.Generic.List`1[<>f__AnonymousType5`2[System.Int32,System.Collections.Generic.ICollection`1[FinalProject.Models.OrderDetail]]]' to type 'System.Collections.Generic.List`1[FinalProject.Models.CustomerOrder]'. – Hen Porcilan Jun 24 '15 at 10:42
  • view part : foreach (var order in (List)ViewBag.OrdersForUser) { order } – Hen Porcilan Jun 24 '15 at 11:02
  • controller part: public ActionResult Details(int id = 0) { Customer customer = db.Customers.Find(id); var qry = from c in db.Customers join o in db.Orders on id equals o.CustomerID where id == o.CustomerID select new CustomerOrder { OrderID = o.OrderID, OrderDetails = o.OrderDetails }; ViewBag.OrdersForUser = qry.ToList() ; if (customer == null) { return HttpNotFound(); } return View(customer); } – Hen Porcilan Jun 24 '15 at 11:03
  • the new class: public class CustomerOrder { public int OrderID { get; set; } public ICollection OrderDetails { get; set; } } – Hen Porcilan Jun 24 '15 at 11:04