2011-07-12 65 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