Muhammad Ateek
Muhammad Ateek

Reputation: 1057

how to pass custom query results to view in asp.net

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; }
    }
}

Upvotes: 2

Views: 3456

Answers (2)

Shyju
Shyju

Reputation: 218782

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

Upvotes: 4

Tarek Abo ELkheir
Tarek Abo ELkheir

Reputation: 1331

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

Upvotes: 0

Related Questions