2

i wants to pass the data from my controller to view. here is code of controller

public ActionResult AllCars()
    {
        try
        {
            int id = System.Web.HttpContext.Current.User.Identity.GetUserId<int>();


            var reservedCars = (from c in db.Cars

                            join o in db.Orders on c.Car_Id equals o.Car_Id
                            join u in db.AspNetUsers on o.Id equals u.Id
                            where u.Id == 5
                            select new
                            {
                                c.Car_Description,
                                c.Car_Id,
                                c.CarMakeId,
                                c.CarModelId,
                                c.Reservation_Status,
                                c.Color
                            });

        return View(reservedCars);
    }
    catch (Exception)
    {

        throw;
    }
}

here is my view file.

@model IEnumerable<FinalProject.Models.ReservedCar>



<table>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Car_Id);
            </td>
        </tr>
     }
    </table>

but when i run my view i got following error The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery but this dictionary requires a model item of type 'System.Collections.IEnumerable`

i know im passing dbquery result to view and my view is expecting it will be iEnumerable. Please guide me how i can achieve thhis task.? i just wants to pass query results into my view and wants to use it.

ModelClass. ReservedCar.cs

namespace FinalProject.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class ReservedCar
    {

        public int Id { get; set; }

        public string Car_Description { get; set; }

        public int Car_Id { get; set; }

        public string CarMakeId { get; set; }

        public string CarModelId { get; set; }
        public string Reservation_Status { get; set; }

        public string Color { get; set; }
    }
}
Muhammad Ateek
  • 986
  • 3
  • 10
  • 24

2 Answers2

4

Currently reservedCars is still a query expression of type IQueryable. It hasn't been evaluated/executed yet. So the type of reservedCars is different than the type your view is strongly typed to(IEnumerable<ReservedCar>). That is the reason you are getting the type mismatch error.

Make sure you are returning a collection of ReservedCar class. You can update the projection part of your LINQ expression to project the result to ReservedCar instead of an anonymous object. Also call ToList() method on the query which will execute the query expression and return the results of that.

 var reservedCars = (from c in db.Cars

                            join o in db.Orders on c.Car_Id equals o.Car_Id
                            join u in db.AspNetUsers on o.Id equals u.Id
                            where u.Id == 5
                            select new ReservedCarVm
                            {
                                CarId = c.Car_Id,
                                Description = c.Car_Description,
                                Color = c.Color
                                // Map other properties as needed.
                            }).ToList();
return View(reserverCars);

Assuming your ReservedCarVm class has Color,Car_Description and Car_Id properties which is of same type of those in the Car entity class like below

public class ReservedCarVm
{
  public string Color { set;get;}
  public string Description { set;get;}
 //Add other properties needed by the view here
}

Now make sure your view is strongly typed to the collection of this view model class

@model IEnumerable<ReserverdCarVm>
@foreach(var item in Model)
{
  <p>@item.CarId</p>
  <p>@item.Color</p>
}
Shyju
  • 197,032
  • 96
  • 389
  • 477
  • still getting error in controller. 'FinalProject.Models.ReservedCar' cannot be constructed in a LINQ to Entities query. Answer is updated. Please check my Model class. – Muhammad Ateek Aug 20 '16 at 17:00
  • Do you have a class called ReserverCar ? How does it looks like ? Does it have the same proeprties as we used ? – Shyju Aug 20 '16 at 17:01
  • check my model class in updated question. yeah it have all these properties. – Muhammad Ateek Aug 20 '16 at 17:03
  • How does the `Car` class looks like ? Do they have the same properties ? – Shyju Aug 20 '16 at 17:05
  • yes i checked closely and i have all these properties in Car model. but still getting following error. Additional information: The entity or complex type 'FinalProject.Models.ReservedCar' cannot be constructed in a LINQ to Entities query. – Muhammad Ateek Aug 20 '16 at 17:19
  • its not working. i check each and every thing. Additional information: The entity or complex type 'FinalProject.Models.ReservedCar' cannot be constructed in a LINQ to Entities query. :-( – Muhammad Ateek Aug 20 '16 at 17:26
  • finaly i resolved my problem. we cant use model object in linq query. Read this issue. to resolve this problem that i was facing. http://stackoverflow.com/questions/12916080/the-entity-or-complex-type-cannot-be-constructed-in-a-linq-to-entities-query – Muhammad Ateek Aug 20 '16 at 17:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121434/discussion-between-shyju-and-muhammad-ateek). – Shyju Aug 20 '16 at 17:42
0

you could do this:

Instead of @model IEnumerable< FinalProject.Models.ReservedCar>
you could do @model IQueryable< FinalProject.Models.ReservedCar>

The above is not recommended, you should not call database directly from the view, I don't see any reason for doing that.

Also you need to dispose the dbcontext, in the code you provided, you are not disposing the dbconext which is not good

just redo your code by this

var reservedCars = (from c in db.Cars

                            join o in db.Orders on c.Car_Id equals o.Car_Id
                            join u in db.AspNetUsers on o.Id equals u.Id
                            where u.Id == 5
                            select new
                            {
                                c.Car_Description,
                                c.Car_Id,
                                c.CarMakeId,
                                c.CarModelId,
                                c.Reservation_Status,
                                c.Color
                            }).ToList();
  db.Dispose();
Tarek Abo ELkheir
  • 1,281
  • 9
  • 12