开发者

asp.net MVC 3 Razor/Linq

开发者 https://www.devze.com 2023-03-19 21:08 出处:网络
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 dictio

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).Or开发者_开发问答derByDescending(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<AtAClick.Models.WhatsOn>

@{ ViewBag.Title = "Index"; }

<h2>Index</h2>

<p>@Html.ActionLink("Create New", "Create")</p>
<table>
    <tr>
        <th>start</th>
        <th>end</th>
        <th>Name</th>
        <th>Desc</th>
        <th>link</th>
        <th>CalenderDisplay</th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>@Html.DisplayFor(modelItem => item.day)</td>
        <td>@Html.DisplayFor(modelItem => item.whtscount)</td>          
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

============================

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


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

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


Your view is expecting an IEnumerable<AtAClick.Models.WhatsOn>, but you are passing a collection of anonymous types. You should change your projection (Select) to instantiate instances of your WhatsOn type instead.

Update

You will need to create a new data transfer object (DTO) type to project onto (e.g. WhatsOnDto), as WhatsOn is an entity type. See The entity cannot be constructed in a LINQ to Entities query for more info. Update your view's model type to an IEnumerable<WhatsOnDto>, or better still a MyViewModel type which contains a property of WhatsOnDtos.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号