in Education by
I'm getting this error: The model item passed into the dictionary is of type System.Data.Entity.Infrastructure.DbQuery``1[<>f__AnonymousType1``2[System.DateTime,System.Int32]], but this dictionary requires a model item of type System.Collections.Generic.IEnumerable``1[AtAClick.Models.WhatsOn]. This is my controller; public ViewResult Index(WhatsOn model) { DateTime myDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); var datequery = db.WhatsOns.Where(c => c.start > myDate).OrderByDescending(c => c.start).GroupBy(c => c.start).Select( sGroup => new { day = sGroup.Key, whtscount = sGroup.Count() }); return View(datequery); } I want to return all entries after todays date and the number of entries. I'm new to this, any help is greatly apprecieted! Thanks in advance, if you need any mjore detail just let me know. Thanks! This is my view ============================== @model IEnumerable @{ ViewBag.Title = "Index"; }

Index

@Html.ActionLink("Create New", "Create")

@foreach (var item in Model) { } ============================ This is the edit method in my controller; // // GET: /WhatsOn/Edit/5 public ActionResult Edit(int id) { WhatsOn whatson = db.WhatsOns.Find(id); return View(whatson); } // // POST: /WhatsOn/Edit/5 [HttpPost] public ActionResult Edit(WhatsOn whatson) { if (ModelState.IsValid) { db.Entry(whatson).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(whatson); } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)
start end Name Desc link CalenderDisplay
@Html.DisplayFor(modelItem => item.day) @Html.DisplayFor(modelItem => item.whtscount) @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID })

1 Answer

0 votes
by
i think the problem is a mismatch between what your view expects and what your controller is passing. In your select statement your selecting a new anonymous type but your view is expecting the type IEnumerable assuming the whats on fields are day and whtscount then replace the datequery with var datequery = db.WhatsOns.Where(c => c.start > myDate).OrderByDescending(c => c.start).GroupBy(c => c.start).Select( sGroup => new WhatsOn() { day = sGroup.Key, whtscount = sGroup.Count() }); Update: The error is indicating that your select query cannot be translated into the equivilent sql, what you could try is changing it to var datequery = db.WhatsOns.Where(c => c.start > myDate).OrderByDescending(c => c.start).GroupBy(c => c.start).AsEnumerable().Select( sGroup => new WhatsOn { day = sGroup.Key, whtscount = sGroup.Count() }); Update: I think the issue may be that when you get to the post method of the edit, the WhatsOn object is no longer associated with the database WhatsOn it was originally loaded from, have a go at changing it to public ActionResult Edit(int id) { WhatsOn whatson = db.WhatsOns.Find(id); return View(whatson); } [HttpPost] public ActionResult Edit(int id, FormCollection collection) { WhatsOn whatsOnmodel = db.WhatsOns.Find(id); if (TryUpdateModel(whatsOnmodel)) { db.SaveChanges(); return RedirectToAction("Index"); } return View(whatsOnmodel ); } Update: If that approach was not working you could see if your one did just add the loading at the beginning so [HttpPost] public ActionResult Edit(int id, WhatsOn whatson) { WhatsOn whatsOnmodel = db.WhatsOns.Find(id); if (ModelState.IsValid) { whatsOnmodel.day = whatson.day; whatsOnmodel.whtscount = whatson.whtscount; db.SaveChanges(); return RedirectToAction("Index"); } return View(whatsOnmodel); } you could test that and see what happens Update: Actually i think your first approach should of worked but i think it requires the Id, what happens if you make it [HttpPost] public ActionResult Edit(int id, WhatsOn whatson) { if (ModelState.IsValid) { db.Entry(whatson).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(whatson); }

Related questions

0 votes
    I'm getting this error: The model item passed into the dictionary is of type System.Data.Entity. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    As you know we can set attributes to actionLink or textBox in razor views but how can we set attributes ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    As you know we can set attributes to actionLink or textBox in razor views but how can we set attributes ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    I am running aspnet_compiler as a post-build event, using the following command: aspnet_compiler.exe -v / - ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I have an Azure table datastore and here's my model for one class: public class Product : ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I am working on vs 2010 and EF 4.1 with SQL server database. Below mentioned code works fine with local SQL ... , Func`1 continuation) Select the correct answer from above options...
asked Feb 3, 2022 in Education by JackTerrance
0 votes
    I'm coding a basic portfolio page in ASP.NET Core Razor Pages. I want to have a contact form at ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have batch of user accounts and every user account's password is not crypted.I want to insert to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have a database (SQL Server) with clients and a MVC plataform, and i want a row to be "hidden ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I need to integrate IDX MLS into a real estate website. Also I want to know how to implement ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    I have a database (SQL Server) with clients and a MVC plataform, and i want a row to be "hidden ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Successful Model Editing without a bunch of hidden fields (5 answers ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I am inserting JSON string into table, than on listing page in View inside foreach loop I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    Say I have a page that display search results. I search for stackoverflow and it returns 5000 results, 10 per page. Now I ... was posted previously etc.. What I'd like to do is...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    I'm looking to build an reusable control or custom helper for my MVC project. I'm sure there is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
...