2011-04-21 74 views
0

我從我在MS SQL Server中創建多個表顯示從與信息(SQL)的視圖數據。所收集的數據以實體框架顯示在asp.net mvc3站點的強類型視圖中。更新視圖(從SQL Server)在asp.net MVC3

在創建編輯頁面都顯示爲預期,這當我試圖挽救我的修改細節的東西不會工作的。

頁面構建

某些代碼

方法顯示編輯頁面

public ActionResult Edit(Guid id) 
{ 
    return View(db.ViewIndividualAlls.Where(x => x.UserGuid == id).SingleOrDefault()); 
} 

方法來接收HttpPost

[HttpPost] 
public ActionResult Edit(Guid id, ViewIndividualAll viewIndividualAll) 
{ 
    // Ind confirmed in the debugger, it works! 
    var ind = db.ViewIndividualAlls.Where(x => x.UserGuid == id).SingleOrDefault(); 
    //var ind = db.ViewIndividualAlls.Find(viewIndividualAll); 
    try 
    { 
     if (TryUpdateModel(ind)) 
     { 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     else 
     { 
      ViewData["error"] = "Model validation failed!"; 
      return View(viewIndividualAll); 
     } 
    } 
    catch (Exception e) 
    { 
     ViewData["error"] = string.Concat(e.Message, " ", e.StackTrace); 
     return View(viewIndividualAll); 
    } 
} 

錯誤消息

The property 'whatever' is part of the object's key information and cannot be modified. 

的怪樣,因爲我能夠更新SQL Server Management Studio中幾乎任何東西,除了與當然的外鍵約束的東西。

任何建議(有關)在所有將不勝感激

謝謝!

更新

我檢查了主鍵在模型瀏覽器,發現他們所有設置錯誤,所以我固定它。現在當我修復,我得到這個新的錯誤

Unable to update the EntitySet 'ViewIndividualAll' because it has a DefiningQuery 
and no <UpdateFunction> element exists in the <ModificationFunctionMapping> 
element to support the current operation. 

所以沒有喜悅,更多的修復,但仍然感興趣的幫助!

回答

0

解決

[HttpPost] 
public ActionResult Edit(Guid id, ViewIndividualAll viewIndividualAll) 
{ 
    var ind = db.ViewIndividualAlls.Find(id); 
    try 
    { 
     if (TryUpdateModel(ind)) 
     { 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     else 
     { 
      ViewData["error"] = "Model validation failed!"; 
      return View(viewIndividualAll); 
     } 
    } 
    catch (Exception e) 
    { 
     ViewData["error"] = string.Concat(e.Message, " ", e.StackTrace, "\n", e.InnerException, "\n", e.HelpLink); 
     return View(viewIndividualAll); 
    } 
} 
1

從它您正在使用的Guid工作作爲重點的樣子。實體框架與Guids混雜爲關鍵。 您得到的錯誤可能是因爲TryUpdateModel(ind)觸及了Guid值。 您應該標記與[ScaffoldColumn(false)]的GUID鍵(不知道這是否會幫助)或手動提交領域從數據庫複製到你的實體。

+0

用的GUID工作通常工作就好了其他地方,但它可能是麻煩與SQL視圖。無論如何,我會給它一個 – 2011-04-21 13:06:42

+0

到目前爲止沒有運氣,也許我把[ScaffoldColumn(false)]放在了錯誤的屬性上。讓我們繼續使用Google! :) – 2011-04-21 13:38:09