简体   繁体   中英

Passing entity framework query to view error

I just created a database with Entity Framework Code first Approach.

Here is my modal class

public class MobileProduct
{
    [Key]
    public int p_id { get; set; }
    public string  p_name { get; set; }
    public string  p_description { get; set; }

    public string p_price { get; set; }
}

Here is my DbContext derived class

public class Entities : DbContext
{
   public DbSet<MobileProduct> Mobiles { get; set; }
}

I just created a query to pass this data t view on an action method

Entities db = new Entities();
public ActionResult Mobiles()
{
    var q = from n in db.Mobiles
            select n;
    return View(q);
}

Here is my View

@model HomeShopping.Models.MobileProduct

@{
    ViewBag.Title = "Mobiles";
}

<h2>Mobiles</h2>

I got an error when accessing the view:

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[HomeShopping.Models.MobileProduct]', but this dictionary requires a model item of type 'HomeShopping.Models.MobileProduct'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[HomeShopping.Models.MobileProduct]', but this dictionary requires a model item of type 'HomeShopping.Models.MobileProduct'.

I presume you want to send a list of MobileProduct to the view? If so; change your model to

@model IEnumerable<HomeShopping.Models.MobileProduct>

@{
    ViewBag.Title = "Mobiles";
}

<h2>Mobiles</h2>

Currently your model only expects one instance of type MobileProduct whereas your Mobiles action is creating a list of MobileProduct. You will also need to evaluate the query in the action by performing a ToList() on it.

public ActionResult Mobiles()
        {
            var q = from n in db.Mobiles
                    select n;
            return View(q.ToList());
        }

You aren't realizing your query so you're sending the query itself, not the results of the query, to the View.

var q = (from n in db.Mobiles
         select n).ToList();
return View(q);

As daveL posted, you also need to change the definition of the model in your view.

@model IEnumerable<HomeShopping.Models.MobileProduct>

Haven't tested, but try defining your model as:

@model IQuerable<HomeShopping.Models.MobileProduct>

If that doesn't work, then perhaps:

@model IEnunerable<HomeShopping.Models.MobileProduct>

...with your object returned as:

View(q.ToList());

you are saving a query to q, not the evaluation of the query. You need a ToList() if you expect multiple results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM