2014-10-09 80 views
-2
if (ModelState.IsValid) 
{ 
using (mydataEntities1 db = new mydataEntities1()) 
db.Contactmodels.Add(model); 
db.SaveChanges(); 


ModelState.Clear(); 
model = null; 
ViewBag.Message = "Thanks for contacting us"; 

錯誤是名稱db在當前上下文中不存在。 請幫我解決這個問題。該名稱在當前上下文中不存在

回答

0

您忘記了您的using塊的花括號。當前的代碼的結構是這樣的:

using (mydataEntities1 db = new mydataEntities1()) 
    db.Contactmodels.Add(model); // inside the block, "db" exists 
db.SaveChanges(); // outside the block, "db" is no longer in scope 

只是整個包住using塊像任何其他代碼塊:

using (mydataEntities1 db = new mydataEntities1()) 
{ 
    db.Contactmodels.Add(model); 
    db.SaveChanges(); 
} 
相關問題