2011-07-12 68 views
2

我得到这个错误:asp.net MVC 3剃须刀/ LINQ的

传递到字典的模型产品System.Data.Entity.Infrastructure.DbQuery``1[<>f__AnonymousType1``2[System.DateTime,System.Int32]]类型,但是这本词典需要System.Collections.Generic.IEnumerable``1[AtAClick.Models.WhatsOn]型的典范项目。

这是我的控制器;

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

我想在今天的日期和条目数之后返回所有条目。我对此很陌生,任何帮助都非常感谢!在此先感谢,如果您需要任何mjore细节,请让我知道。谢谢!

这是我的视图

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

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

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

这是我的控制器中的编辑方法;

// // GET:/ WhatsOn /编辑/ 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); 
    } 
+0

你能编辑你的文章并包含你的视图的代码吗? – Jon

+0

是的,我现在将它弹出...完成。编辑hasd拿出了表格标签。它实际上有点令人费解,但是其中的foreach就在那里。 – Dan

回答

0

我认为这个问题是你的看法,并期待你的控制器传递之间的不匹配。

在SELECT语句的选择新的匿名类型,但你的观点是期待型IEnumerable<WhatsOn>

假设什么在字段一天whtscount然后用

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

更新替换datequery: 错误表明您的选择查询无法转换为equivilent sql,您可以尝试将其更改为

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

更新:我认为这个问题可能是,当你到达编辑的post方法时,WhatsOn对象不再与它最初加载的数据库WhatsOn相关联,请将其更改为

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

更新:如果这种方法是行不通的,你可以看到,如果你没有一个刚刚开始添加加载因此

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

你可以测试看看会发生什么

更新: 其实我觉得你的第一个方法应该的工作,但我认为它需要的身份证,会发生什么,如果你把它

[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); 
} 
+0

感谢您的回复,thst让我进一步了一点,但现在我得到了这个错误...实体或复杂类型'AtAClick.Models.WhatsOn'不能在LINQ to Entities查询中构造。我必须编辑视图中的foreach来处理这个查询吗?再次感谢! – Dan

+0

这工作无需使用DTO。它按日期分组,并统计数量!但是,当我尝试编辑一个条目时,我得到这个错误,随时停止帮助我,顺便说一句,我知道这是错误后错误..无论如何,商店更新,插入或删除语句影响了意想不到的行数(0 )。自实体加载后,实体可能已被修改或删除。刷新ObjectStateManager条目...有什么建议吗?我会以任何一种方式标记你的答案! – Dan

+0

@Dan你会介意在你试图更新记录的地方发布你的控制器的post方法 – Manatherin

0

您的视图期待一个IEnumerable<AtAClick.Models.WhatsOn>,但是你传递了一个匿名类型的集合。您应该更改投影(Select)以实例化WhatsOn类型的实例。

更新

您需要创建一个新的数据传输对象(DTO)型投影到(例如WhatsOnDto),作为WhatsOn是一个实体类型。有关更多信息,请参阅The entity cannot be constructed in a LINQ to Entities query。将您的视图的模型类型更新为IEnumerable<WhatsOnDto>,或更好的MyViewModel类型,其中包含WhatsOnDtos的属性。

+0

谢谢,我认为这就是Manatheirn也建议的。我实施了他的解决方案,但得到了我刚刚在他的回答下发表的评论中的错误。任何帮助表示赞赏。谢谢! – Dan

+0

我已经更新了我的答案 – devdigital

+0

我现在就试试这个,回报,再次感谢,我非常感谢! 2分钟! – Dan